gpt4 book ai didi

mysql - sql 查询,在 where 类中进行行比较

转载 作者:行者123 更新时间:2023-11-29 09:03:23 25 4
gpt4 key购买 nike

我有两个表:CustomersProducts。一位客户可以拥有多个产品。

我正在尝试检索没有特定产品的客户。

例如,10 位客户购买了产品 AB,另外 10 位客户购买了 AB,和 C。如何找回那些没有 C 产品的客户?

最佳答案

对于您当前的数据库结构,这就是您正在寻找的:

select c.id, c.name, c.phone, c.address 
from Customers c
where not exists (select * from products p
where p.customer_id = c.id and p.id = 'c')

但是,您应该考虑创建第三个表来存储单独的购买。

关于mysql - sql 查询,在 where 类中进行行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7875747/

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