在单一 MySQL 查询中为所有列名称设置“别名”
要为列名称设置别名,语法如下 −
select yourColumnName1 anyAliasName1,yourColumnName2 anyAliasName2 from yourTableName anyAliasName;
为了理解上述语法,让我们创建一个表 −
mysql> create table DemoTable2014 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.70 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2014 values('John','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2014 values('David','Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable2014 values('John','Doe'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2014 values('Chris','Brown'); Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2014;
这将产生以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +-----------+----------+ 4 rows in set (0.00 sec)
以下是为单一 MySQL 查询中的列名称设置“别名”的查询 −
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)
广告