力扣 (LeetCode) - Database-刷題183--從不訂購的客戶

妙手書生2016發表於2020-11-22

題目描述:

某網站包含兩個表,Customers 表和 Orders 表。編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
例如給定上述表格,你的查詢應返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+


我的答案:

select c.Name as Customers
from Customers c
left outer join Orders o on o.CustomerId = c.Id
where o.CustomerId is null

答案解析:

表連線聯合查詢,左連線判斷相關列的value值為null

相關文章