通过使用 SQL,可以为表名称或列名称指定别名(Alias)。
SELECT column_name AS alias_name
FROM table_name;
WHERE [condition];
SELECT column_name(s)
FROM table_name AS alias_name;
WHERE [condition];
在本教程中,我们将使用著名的Northwind示例数据库。
以下是"Customers" 表中的数据:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
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 |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
下面是选自 "Orders" 表的数据:
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10354 | 58 | 8 | 1996-11-14 | 3 |
10355 | 4 | 6 | 1996-11-15 | 1 |
10356 | 86 | 6 | 1996-11-18 | 2 |
以下SQL语句创建两个别名,一个用于CustomerID列,另一个用于CustomerName列:
以下SQL语句创建两个别名,一个用于CustomerName列,一个用于ContactName列。注:如果别名包含空格,则需要双引号或方括号:
以下SQL语句创建一个名为“Address”的别名,它包含四列(Address,PostalCode,City and Country):
SELECT CustomerName, Address + ", " + PostalCode + ", " + City + ", " + Country AS Address
FROM Customers;
注意:要使上面的SQL语句在MySQL中工作,请使用以下命令:
SELECT CustomerName, CONCAT(Address,", ",PostalCode,", ",City,", ",Country) AS Address
FROM Customers;
以下SQL语句选择CustomerID = 4(“围绕角”)的所有订单。我们使用“Customers”和“Orders”表,给它们分别为“c”和“o”的表别名(这里我们使用别名来使SQL更短):
以下SQL语句与上述相同,但没有别名:
在下列情况下使用别名是有用的:
查询涉及多个表
用于查询函数
需要把两个或更多的列放在一起
列名长或可读性差
考虑下面两个数据表:
(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 |
+-------------+---------------+
SQL MIN() FunctionMIN() 函数MIN() 函数返回所选列的最小值。SQL MIN() 语法SELECT MIN(column_name)FROM table_nameWHER...
学习C++-C++ bool类型C++添加了一种称为bool的新类型。一个布尔变量是一个值为true或false的变量。您可以使用bool类型来表示true...
学习C++-C++指针以下代码使用&运算符来查找地址。#include iostream using namespace std; int main() {int donuts = 6;double ...
学习C++-C++控制台读取字符到文件结尾#include iostream int main() {using namespace std;char ch;int count = 0;cin.get(ch);/...
学习C++-C++函数参数函数参数和值传递C++通常通过值传递参数。例如, double volume = cube(side); 这边是一个变量,在运行中,...
C++ 指针运算符(& 和 *) C++ 运算符C++ 提供了两种指针运算符,一种是取地址运算符 ,一种是间接寻址运算符 *。指针是一个包含...