如何在 MySQL SELECT 中生成字段?
mysqlmysqli database更新于 2025/7/29 10:52:17
为此使用关键字 AS。让我们首先创建一个表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (3.16 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(Name) values('John');
Query OK, 1 row affected (0.50 sec)
mysql> insert into DemoTable(Name) values('Robert');
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable(Name) values('David');
Query OK, 1 row affected (0.54 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Robert | | 3 | David | +----+--------+ 3 rows in set (0.00 sec)
以下是在 MySQL SELECT 中生成字段的查询 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

