当前位置: 移动技术网 > IT编程>数据库>Mysql > LeetCode——Customers Who Never Order(灵活使用NOT IN以及IN)

LeetCode——Customers Who Never Order(灵活使用NOT IN以及IN)

2019年10月10日  | 移动技术网IT编程  | 我要评论
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       |
+-----------+

此题,竟然一时间没想到如何合理的解决方案,主要是有较长的时间没有使用innot in.
sql也是一个手熟的活,需要经常锻炼,以下是解题答案:

# write your mysql query statement below
select customers.name as customers 
from customers
where customers.id not in (select customerid from orders)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网