如何选择 * 并重命名 MySQL 中的列?
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Age int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(Name,Age) values('John',21); mysql> insert into DemoTable(Name,Age) values('Carol',24); mysql> insert into DemoTable(Name,Age) values('David',22); mysql> insert into DemoTable(Name,Age) values('Sam',23);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 21 | | 2 | Carol | 24 | | 3 | David | 22 | | 4 | Sam | 23 | +----+-------+------+ 4 rows in set (0.00 sec)
以下是对SELECT*和重命名列的查询-
mysql> select tbl.*,Name AS StudentName from DemoTable tbl;
这将产生以下输出-
+----+-------+------+-------------+ | Id | Name | Age | StudentName | +----+-------+------+-------------+ | 1 | John | 21 | John | | 2 | Carol | 24 | Carol | | 3 | David | 22 | David | | 4 | Sam | 23 | Sam | +----+-------+------+-------------+ 4 rows in set (0.00 sec)