SQL 别名

SQL 别名

通过使用 SQL,可以为表名称或列名称指定别名。

基本上,创建别名是为了让列名称的可读性更强。

列的 SQL 别名语法

SELECT column_name AS alias_name
FROM table_name;

表的 SQL 别名语法

SELECT column_name(s)
FROM table_name AS alias_name;
演示数据库

在本教程中,我们将使用 RUNOOB 样本数据库。

下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 芝麻教程      | http://www.web3.xin/    | 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, CONCAT(url, ', ', alexa, ', ', country) AS site_info
FROM Websites;
执行输出结果:
mysql> 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.web3.xin/, 5000, USA   |
| 微博          | http://weibo.com/, 20, CN         |
| Facebook      | https://www.facebook.com/, 3, USA |
| stackoverflow | NULL                              |
+---------------+-----------------------------------+
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://www.web3.xin/ |   100 | 2016-05-13 |
| 芝麻教程 | http://www.web3.xin/ |   220 | 2016-05-15 |
| 芝麻教程 | http://www.web3.xin/ |   201 | 2016-05-17 |
+----------+----------------------+-------+------------+
3 rows 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://www.web3.xin/ |   100 | 2016-05-13 |
| 芝麻教程 | http://www.web3.xin/ |   220 | 2016-05-15 |
| 芝麻教程 | http://www.web3.xin/ |   201 | 2016-05-17 |
+----------+----------------------+-------+------------+
3 rows in set (0.00 sec)

在下面的情况下,使用别名很有用:

  • 在查询中涉及超过一个表
  • 在查询中使用了函数
  • 列名称很长或者可读性差
  • 需要把两个列或者多个列结合在一起
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2024

备案号:京ICP备15003423号-3