您的位置:首页 > 其它

Database Leetcode Customers Who Never Order

2016-02-18 17:07 351 查看
题目:

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: