mysql - 匹配所有值 "IN"sql查询

标签 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

抱歉我的英语不太好。但我希望你能明白我想要什么。

最佳答案

使用聚合和具有:

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/

相关文章:

MySQL:如何查询并排显示两个用户的答案

mysql - sql 中不区分大小写的 order_by - Codeigniter

PHP/MYSQL 将句子与关键字匹配?

php - 如何调试MySQL错误 "#1064 - You have an error in your SQL syntax"

sql - 长期 Oracle 开发人员/DBA 学习 SQL Server - 书籍推荐吗?

sql - 如何在 SQL Server 中将数据库名称作为参数传递

MySQL从表A和表B插入表C

mysql - SQL - 如何使用 UNION 将三个 INNER JOIN 结果连接到一个结果表中?

php - 归档 php sql date()

php - 在 mysql 中获得更快记录搜索的最佳方法