欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 20:44:34
此题,竟然一时间没想到如何合理的解决方案,主要是有较长的时间没有使用 与`NOT IN`. 也是一个手熟的活,需要经常锻炼,以下是解题答案: ......
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)