W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
通過使用 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示例數(shù)據(jù)庫。
以下是"Customers" 表中的數(shù)據(jù):
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" 表的數(shù)據(jù):
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語句創(chuàng)建兩個(gè)別名,一個(gè)用于CustomerID列,另一個(gè)用于CustomerName列:
以下SQL語句創(chuàng)建兩個(gè)別名,一個(gè)用于CustomerName列,一個(gè)用于ContactName列。注:如果別名包含空格,則需要雙引號(hào)或方括號(hào):
以下SQL語句創(chuàng)建一個(gè)名為“Address”的別名,它包含四列(Address,PostalCode,City and Country):
SELECT CustomerName, Address + ', ' + PostalCode + ', ' + City + ', ' + Country AS Address
FROM Customers;
注意:要使上面的SQL語句在MySQL中工作,請(qǐng)使用以下命令:
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;
以下SQL語句選擇CustomerID = 4(“圍繞角”)的所有訂單。我們使用“Customers”和“Orders”表,給它們分別為“c”和“o”的表別名(這里我們使用別名來使SQL更短):
以下SQL語句與上述相同,但沒有別名:
在下列情況下使用別名是有用的:
查詢涉及多個(gè)表
用于查詢函數(shù)
需要把兩個(gè)或更多的列放在一起
列名長或可讀性差
考慮下面兩個(gè)數(shù)據(jù)表:
?。╝)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 |
+----+----------+-----+-----------+----------+
?。╞)另一個(gè)是 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;
上面語句的運(yùn)行結(jié)果如下所示:
+----+----------+-----+--------+
| 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;
其運(yùn)行結(jié)果如下所示:
+-------------+---------------+
| CUSTOMER_ID | CUSTOMER_NAME |
+-------------+---------------+
| 1 | Ramesh |
| 2 | Khilan |
| 3 | kaushik |
| 4 | Chaitali |
| 5 | Hardik |
| 6 | Komal |
| 7 | Muffy |
+-------------+---------------+
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: