我们可以从 MySQL 中的另一个表向一个表添加一列吗?

我们可以从 MySQL 中的另一个表向一个表添加一列吗?

是的,我们可以从另一个表向一个表添加一列。让我们首先创建两个表。创建表的查询如下 -

mysql> create table FirstTable -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (1.48 sec)登录后复制

mysql> create table SecondTable -> ( -> UserId int, -> UserAge int -> ); Query OK, 0 rows affected (1.57 sec)登录后复制

mysql> ALTER TABLE FirstTable ADD COLUMN Age TINYINT UNSIGNED DEFAULT 0; Query OK, 0 rows affected (1.53 sec) Records: 0 Duplicates: 0 Warnings: 0登录后复制

mysql> UPDATE FirstTable tbl1 -> INNER JOIN SecondTable tbl2 ON tbl1.UserId = tbl2.UserId -> SET tbl1.Age = tbl2.UserAge; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0登录后复制

mysql> desc FirstTable;登录后复制

+----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+-------+ | UserId | int(11) | YES | | NULL | | | UserName | varchar(20) | YES | | NULL | | | Age | tinyint(3) unsigned | YES | | 0 | | +----------+---------------------+------+-----+---------+-------+ 3 rows in set (0.53 sec)登录后复制