SQL刷题日记22.7.22——183. 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/customers-who-never-order

本题思路有两个
第一个就是很容易想到把两个表连接起来,然后找出来orders表里没有数据的就行了。也就是orders.id is null

select name as Customers
from customers
    left join orders on customers.Id = customerId
where orders.id is null

另一个思路。可以想到我们要找到没有订单的用户,其实就是找到全部的用户去掉有订单的用户,也就是说可以用not in的方式来查询。

select name as Customers
from Customers
where id not in (
    select CustomerId 
    from orders
)
posted @   Eumenides丶  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示