gpt4 book ai didi

sql - 查找未订购所有 3 种比萨饼的顾客

转载 作者:行者123 更新时间:2023-12-04 18:23:30 26 4
gpt4 key购买 nike

customers:
+-----------+-----------+
| cid | name |
+-----------+-----------+
| 1 | a |
| 2 | b |
| 3 | c |
+-----------+-----------+

pizza:
+-----------+-----------+
| pid | type |
+-----------+-----------+
| 1 | sausage |
| 2 | cheese |
| 3 | veggies |
| 4 | sausage |
| 5 | veggies |
| 6 | sausage |
| 7 | sausage |
+-----------+-----------+

orders:
+-----------+-----------+-----------+
| oid | cid | pid |
+-----------+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 3 | 4 |
| 5 | 1 | 5 |
| 6 | 3 | 6 |
| 7 | 3 | 7 |
+-----------+-----------+-----------+

我在理解 sql 逻辑时遇到了一些麻烦。我如何找到没有订购所有 3 种披萨的顾客?这三种类型是香肠、奶酪和蔬菜。我需要使用 NOT EXIST 吗?

最佳答案

您可以使用 having 子句来查找订购少于 3 种披萨的客户:

select  c.name
from customers c
join orders c
on c.cid = c.id
join pizza p
on p.id = c.pid
group by
c.name
having
count(distinct p.type) < 3

关于sql - 查找未订购所有 3 种比萨饼的顾客,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13459017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com