gpt4 book ai didi

Mysql 查询 WHERE 与 AND

转载 作者:行者123 更新时间:2023-11-29 06:16:10 26 4
gpt4 key购买 nike

我有一张 table

id|customer_id|comment
1 34 good
2 23 bad
3 34 regular
4 76 longterm
5 34 bad
6 23 good

我们可以看到一个 customer_id 有不同的评论(例如 – 34 有好的、常规的、坏的)我正在尝试从上表中提取记录,其中评论为“好,也坏”并按 customer_id 分组,因此在结果中我应该看到记录 id => 1, 5, 2, 6

有人可以帮我写这个 where 子句吗?

问候

最佳答案

要让一行数据引用另一行,您需要联接。在这种情况下,您需要加入针对自身的评论:

select distinct a.customer_id 
from comments a
join comments b on a.customer_id = b.customer_id and a.id < b.id
where a.comment = 'good'
and b.comment = 'bad'

我选择了a.id < b.id语法以避免在两个方向上执行相同的连接,从而导致重复工作。通过指定 unique,您将只能看到每个客户一次。

关于Mysql 查询 WHERE 与 AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6362713/

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