在单个MySQL查询中为所有列名设置“别名”
要为列名设置别名,语法如下:
select yourColumnName1 anyAliasName1,yourColumnName2 anyAliasName2 from yourTableName anyAliasName;
要了解上述语法,让我们创建一个表-
create table DemoTable2014 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable2014 values('John','Smith'); insert into DemoTable2014 values('David','Miller'); insert into DemoTable2014 values('John','Doe'); insert into DemoTable2014 values('Chris','Brown');
使用select语句显示表中的所有记录-
select *from DemoTable2014;
这将产生以下输出-
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +-----------+----------+ 4 rows in set (0.00 sec)
这是在单个MySQL查询中为列名设置“别名”的查询-
select FirstName StudentFirstName,LastName StudentLastName -> from DemoTable2014 tbl;
这将产生以下输出-
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +------------------+-----------------+ 4 rows in set (0.00 sec)