<span>[LeetCode#183]Customers Who Never Order</span>

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左连接,并且条件是customerId为空的客户:

SELECT NAME as Customers FROM customers as a
LEFT JOIN orders as b on a.Id = b.CustomerId
WHERE b.CustomerId is NULL;

  

解法二:

  使用的方法是使用not in :

SELECT Name as Custmoers FROM customers as a 
WHERE a.Id not in 
(SELECT b.CustomerId FROM orders as b);

  

全部评论

相关推荐

激昂墓志铭_终章:亚新经典实习3300,转正7k外包。去那干啥,还要加班
投递亚信科技(中国)有限公司等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务