首页 >
文章列表 > 如何查询 MySQL 中 refund_id 相同且 return_code 为 SUCCESS 的记录,并筛选出 return_code=SUCCESS 出现次数大于或等于 2 的记录?
如何查询 MySQL 中 refund_id 相同且 return_code 为 SUCCESS 的记录,并筛选出 return_code=SUCCESS 出现次数大于或等于 2 的记录?
237
2024-12-20

mysql 查询两个字段特定值且重复记录
如何查询 refund_id 相同,且 return_code 为 success 的记录,并且该记录中的 return_code=success 出现大于或等于 2 条重复记录?
解决方案:
使用以下 sql 查询:
select * from 表 where refund_id in (
select refund_id from 表
where return_code = 'SUCCESS'
group by refund_id
having count(*) >= 2
);
此查询通过以下步骤实现:
- 内部查询 select refund_id from 表... 找到所有 return_code 为 success 的 refund_id,并按 refund_id 分组。
- having count(*) >= 2 子句过滤出出现次数大于或等于 2 的 refund_id 组。
- 外部查询 select * from 表... 使用 in 操作符将内部查询的子结果作为 refund_id 的筛选条件,返回符合条件的记录。
因此,执行此查询将返回 refund_id 相同,且 return_code=success 的记录,并且该记录中的 return_code=success 出现至少 2 条重复记录。