【mysql】沒有購買的客戶
阿新 • • 發佈:2018-11-08
某網站包含兩個表,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 name as customers from customers where id not in( select customerid from orders)
1.查出訂單表中購買過的使用者,
2.使用where條件把購買過的使用者從客戶表中排除,執行的結果就是未購買過的。