在不使用JOIN的情况下模拟LEFT JOIN

时间:2017-02-19 14:21:04

标签: mysql join left-join

我想知道是否可以在不使用LEFT JOIN的情况下模拟JOINs

我使用this database并尝试使用JOIN模拟此SELECT

FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

我目前只能获得INNER JOIN。这是我目前的查询:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID=Orders.CustomerID
ORDER BY CustomerName;

1 个答案:

答案 0 :(得分:2)

您可以添加左反连接查询,例如:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID=Orders.CustomerID
ORDER BY CustomerName

UNION

SELECT Customers.CustomerName, NULL FROM Customers 
    WHERE Customers.CustomerID NOT IN (SELECT CustomerID FROM Orders);