通过使用 SQL,可以为表名称或列名称指定别名。
基本上,创建别名是为了让列名称的可读性更强。
SELECT column_name AS alias_name
FROM table_name;
SELECT column_name(s)
FROM table_name AS alias_name;
在本教程中,我们将使用 shulanxt 样本数据库。
下面是选自 “Websites” 表的数据:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 树懒学堂 | http://www.shulanxt.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
下面是 “access_log” 网站访问记录表的数据:
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。提示:如果列名称包含空格,要求使用双引号或方括号:
实例:
SELECT name AS n, country AS c
FROM Websites;
输出结果:
+--------------+---------+
| n | c |
+--------------+---------+
| Google | USA |
| 淘宝 | CN |
| 树懒学堂 | CN |
| 微博 | CN |
| Facebook | USA |
| stackoverflow | IND |
+---------------+---------+
在下面的 SQL 语句中,我们把三个列(url、alexa 和 country)结合在一起,并创建一个名为 “site_info” 的别名:
实例:
SELECT name, CONCAT(url, ', ', alexa, ', ', country) AS site_info
FROM Websites;
输出结果:
+--------------+---------------------------------+
| name | site_info |
+--------------+---------------------------------+
| Google | https://www.google.cm/,1,USA |
| 淘宝 | https://www.taobao.com/,13,CN |
| 树懒学堂 | http://www.shulanxt.com/ ,4689,CN |
| 微博 | http://weibo.com/,20,CN |
| Facebook | https://www.facebook.com/,3,USA |
| stackoverflow | http://stackoverflow.com/,0,IND |
+---------------+---------------------------------+
下面的 SQL 语句选取 “菜鸟教程” 的所访问记录。我们使用 “Websites” 和 “access_log” 表,并分别为它们指定表别名 “w” 和 “a”(通过使用别名让 SQL 更简短):
实例:
SELECT w.name, w.url, a.count, a.date
FROM Websites AS w, access_log AS a
WHERE a.site_id=w.id and w.name="菜鸟教程";
输出结果:
+--------------+---------------------------+-------+-------------+
| name | url | count | date |
+--------------+---------------------------+-------+-------------+
| 树懒学堂 | http://www.shulanxt.com/ | 100 | 2016-05-13 |
| 树懒学堂 | http://www.shulanxt.com/ | 220 | 2016-05-15 |
| 树懒学堂 | http://www.shulanxt.com/ | 201 | 2016-05-17 |
+---------------+---------------------------+-------+------------+
不带别名的相同的 SQL 语句:
实例:
SELECT Websites.name, Websites.url, access_log.count, access_log.date
FROM Websites, access_log
WHERE Websites.id=access_log.site_id and Websites.name="菜鸟教程";
输出结果:
+--------------+---------------------------+-------+-------------+
| name | url | count | date |
+--------------+---------------------------+-------+-------------+
| 树懒学堂 | http://www.shulanxt.com/ | 100 | 2016-05-13 |
| 树懒学堂 | http://www.shulanxt.com/ | 220 | 2016-05-15 |
| 树懒学堂 | http://www.shulanxt.com/ | 201 | 2016-05-17 |
+---------------+---------------------------+-------+------------+
在下面的情况下,使用别名很有用:
在查询中涉及超过一个表
在查询中使用了函数
列名称很长或者可读性差
需要把两个列或者多个列结合在一起