如何在MySQL SELECT中生成字段?
为此使用关键字AS。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Name) values('John'); mysql> insert into DemoTable(Name) values('Robert'); mysql> insert into DemoTable(Name) values('David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Robert | | 3 | David | +----+--------+ 3 rows in set (0.00 sec)
以下是在MySQLSELECT中生成字段的查询-
mysql> select Id,Name,'US' AS CountryName from DemoTable;
输出结果
+----+--------+-------------+ | Id | Name | CountryName | +----+--------+-------------+ | 1 | John | US | | 2 | Robert | US | | 3 | David | US | +----+--------+-------------+ 3 rows in set (0.00 sec)