我在返回所有匹配的“ 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
对不起,我的英语不太好。但我希望你明白我想要的。
答案 0 :(得分:0)
使用聚合和having
:
select payment_history
from payment_histories
where payment_status_id in (2, 3)
group by payment_history
having count(distinct payment_status_id) = 2;