如何使用 MySQL SELECT 向已创建的表中添加列?
我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.49 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(Name,Age) values('Robert',24); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name,Age) values('Chris',22); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
输出
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 1 | Robert | 24 | | 2 | Chris | 22 | +----+--------+------+ 2 rows in set (0.00 sec)
以下是在使用 SELECT 时添加新列的查询 −
mysql> select Id,Name,Age,'US' AS DefaultCountryName from DemoTable;
输出
+----+--------+------+--------------------+ | Id | Name | Age | DefaultCountryName | +----+--------+------+--------------------+ | 1 | Robert | 24 | US | | 2 | Chris | 22 | US | +----+--------+------+--------------------+ 2 rows in set (0.00 sec)
广告