题目:
Suppose that a website contains two tables, the Customers
table and the Orders
table. Write a SQL query to find all customers who never order anything.
Table: Customers
.
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Table: Orders
.
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
Using the above tables as example, return the following:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
题意:
给定两个表Customers和Orders,分别表示的是用户表和订购物品表,要求找出那些没有订购过物品的用户姓名。也就是说,就是找出那些在Customers表中的Id在Orders中没有出现过的用户姓名。考虑用not in来做。
代码如下:
select Name as Customers from Customers where Id not in (select CustomerId from Orders);