2020-04-10

查询从不

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Screen Shot 2020-04-10 at 9.54.50 AM.png

Solution 1

*LEFT JOIN,IS NULL*
SELECT Name as Customers FROM Customers
LEFT JOIN Orders 
ON Customers.Id=Orders.CustomerId
WHERE Orders.Id IS null;

左连接,结果晒选不为空的 “IS null”

Solution 2

*NOT IN* 
Select name as Customers
From Customers
Where id not in (select CustomerId from orders)

你可能感兴趣的:(2020-04-10)