匹配所有值“ IN”的sql查询

时间:2019-09-20 02:45:11

标签: mysql sql

我在返回所有匹配的“ 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

对不起,我的英语不太好。但我希望你明白我想要的。

1 个答案:

答案 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;