SQL JOIN 子句用于将数据库中两个或者两个以上表中的记录组合起来。
SQL 中有多种不同的连接:
- 内连接(INNER JOIN):当两个表中都存在匹配时,才返回行。
- 左连接(LEFT JOIN):返回左表中的所有行,即使右表中没有匹配的行。
- 右连接(RIGHT JOIN):返回右表中的所有行,即使左表中没有匹配的行。
- 全连接(FULL JOIN):只要某一个表存在匹配,就返回行。
- 笛卡尔连接(CARTESIAN JOIN):返回两个或者更多的表中记录集的笛卡尔积。
INNER JOIN 关键字(内部连接)
内部链接INNER JOIN关键字选择两个表中具有匹配值的记录。
SQL INNER JOIN 语法:
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
注释:INNER JOIN 与 JOIN 是相同的。
以下SQL语句将返回所有下订单的客户:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
注释:如果表中至少有一个匹配项,INNER JOIN 关键字将返回一行。如果 “Customers” 表中的行与”Orders” 不匹配,则不会列出行。
加入三张表,以下SQL语句选择包含客户和货运单信息的所有订单:
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
LEFT JOIN 关键字(左连接)/RIGHT JOIN 关键字(右连接)
SQL左链接LEFT JOIN关键字返回左表中的所有行,即使在右表中没有匹配。如果在正确的表中没有匹配,结果是NULL。
SQL LEFT JOIN 语法:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
注释:在一些数据库中,LEFT/RIGHT JOIN 称为 LEFT/RIGHT OUTER JOIN。
以下SQL语句将选择所有客户以及他们可能拥有的任何订单:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
注释:LEFT JOIN 关键字返回左表(Customers)中的所有行,即使在右边表(Orders)中没有匹配。
FULL OUTER JOIN 关键字(全连接)
当左或右表记录匹配时,FULL OUTER JOIN关键字将返回所有记录。
注意: FULL OUTER JOIN可能会返回非常大的结果集!
SQL FULL OUTER JOIN 语法:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
以下SQL语句选择所有客户和所有订单:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
注意: FULL OUTER JOIN关键字返回左表(Customers)中的所有行,以及右表(Orders)中的所有行。如果 “Customers”中的行中没有”Orders”中的匹配项,或者”Orders”中的行中没有 “Customers”中的匹配项,那么这些行也会列出。
如果所用的数据库不支持全连接,比如 MySQL,那么可以使用 UNION ALL子句来将左连接和右连接结果组合在一起:
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION ALL
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
笛卡尔连接
笛卡尔连接返回两个或者更多的连接表中记录的笛卡尔乘积。也就是说,它相当于连接谓词总是为真或者缺少连接谓词的内连接。
笛卡尔连接的基本语法:
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
示例:
考虑如下两个表格,(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 | 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 ID, NAME, AMOUNT, DATE
FROM CUSTOMERS, ORDERS;
上述语句将会产生如下结果:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 1 | Ramesh | 3000 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1500 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1560 | 2009-11-20 00:00:00 |
| 1 | Ramesh | 2060 | 2008-05-20 00:00:00 |
| 2 | Khilan | 3000 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 2 | Khilan | 2060 | 2008-05-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 2060 | 2008-05-20 00:00:00 |
| 4 | Chaitali | 3000 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | 3000 | 2009-10-08 00:00:00 |
| 5 | Hardik | 1500 | 2009-10-08 00:00:00 |
| 5 | Hardik | 1560 | 2009-11-20 00:00:00 |
| 5 | Hardik | 2060 | 2008-05-20 00:00:00 |
| 6 | Komal | 3000 | 2009-10-08 00:00:00 |
| 6 | Komal | 1500 | 2009-10-08 00:00:00 |
| 6 | Komal | 1560 | 2009-11-20 00:00:00 |
| 6 | Komal | 2060 | 2008-05-20 00:00:00 |
| 7 | Muffy | 3000 | 2009-10-08 00:00:00 |
| 7 | Muffy | 1500 | 2009-10-08 00:00:00 |
| 7 | Muffy | 1560 | 2009-11-20 00:00:00 |
| 7 | Muffy | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
自连接
自联接是一种常规联接,但表本身是连接的。
Self JOIN语法:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
以下SQL语句匹配来自同一城市的客户:
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;