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