作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一张 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/
我是一名优秀的程序员,十分优秀!