通过使用 SQL,可以为表名称或列名称指定别名(Alias)。
SQL 别名(Aliases)
通过使用 SQL,可以为表名称或列名称指定别名(Alias)。
基本上,创建别名是为了让列名称的可读性更强。
列的 SQL Alias 语法
SELECT column_name AS alias_name
FROM table_name;
FROM table_name;
表的 SQL Alias 语法
SELECT column_name(s)
FROM table_name AS alias_name;
FROM table_name AS alias_name;
演示数据库
在本教程中,我们将使用众所周知的 Northwind 样本数据库。
下面是选自 "Customers" 表的数据:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
下面是选自 "Orders" 表的数据:
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10643 | 1 | 6 | 1997-08-25 | 1 |
10644 | 88 | 3 | 1997-08-25 | 2 |
10645 | 34 | 4 | 1997-08-26 | 1 |
列的 Alias 实例
下面的 SQL 语句指定了两个别名,一个是 CustomerName 列的别名,一个是 ContactName 列的别名。提示:如果列名称包含空格,要求使用双引号或方括号:
实例
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
FROM Customers;
在下面的 SQL 语句中,我们把四个列(Address、City、PostalCode 和 Country)结合在一起,并创建一个名为 "Address" 的别名:
实例
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS Address
FROM Customers;
FROM Customers;
表的 Alias 实例
下面的 SQL 语句选取来自客户 "Alfreds Futterkiste" 的所有订单。我们使用 "Customers" 和 "Orders" 表,并分别为它们指定表别名 "c" 和 "o"(通过使用别名让 SQL 更简短):
实例
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Alfreds Futterkiste';
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Alfreds Futterkiste';
不带别名的相同的 SQL 语句:
实例
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Alfreds Futterkiste';
FROM Customers, Orders
WHERE Customers.CustomerName='Alfreds Futterkiste';
在下面的情况下,使用别名很有用:
- 在查询中涉及超过一个表
- 在查询中使用了函数
- 列名称很长或者可读性差
- 需要把两个列或者多个列结合在一起
语法:
表别名的基本语法如下:
SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];
列别名的基本语法如下:
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
示例:
考虑下面两个数据表,(a)CUSTOMERS 表,如下:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
(b)另一个是 ORDERS 表,如下所示:
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
下面是表别名的用法:
SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT
FROM CUSTOMERS AS C, ORDERS AS O
WHERE C.ID = O.CUSTOMER_ID;
上面语句的运行结果如下所示:
+----+----------+-----+--------+
| ID | NAME | AGE | AMOUNT |
+----+----------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Khilan | 25 | 1560 |
| 4 | Chaitali | 25 | 2060 |
+----+----------+-----+--------+
下面是列别名的用法:
SQL> SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
FROM CUSTOMERS
WHERE SALARY IS NOT NULL;
其运行结果如下所示:
+-------------+---------------+
| CUSTOMER_ID | CUSTOMER_NAME |
+-------------+---------------+
| 1 | Ramesh |
| 2 | Khilan |
| 3 | kaushik |
| 4 | Chaitali |
| 5 | Hardik |
| 6 | Komal |
| 7 | Muffy |
+-------------+---------------+