我在返回所有匹配“IN”条件时遇到问题。
例如我有这样的数据:
table payment_history
id order_id payment_status_id
1 1 2
2 1 3
3 2 2
当我尝试时
SELECT * from payment_histories where payment_status_id in (2, 3)
这是返回 order_id (1, 2)
,但这不是我想要的。
我想要什么时候payment_status_id in (2, 3)
,只需返回 order_id = 1
。因为 orde_id = 1 具备所有条件,
但 order_id = 2 没有 payment_status_id 3
抱歉我的英语不太好。但我希望你能明白我想要什么。
最佳答案
使用聚合和具有
:
select payment_history
from payment_histories
where payment_status_id in (2, 3)
group by payment_history
having count(distinct payment_status_id) = 2;
关于mysql - 匹配所有值 "IN"sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021128/