如何查询存在特定值且出现两次的记录?

如何查询存在特定值并出现两次的记录
问题:
如何使用 mysql 查询特定字段为给定值,且另一个字段的值在表中至少出现两次的记录?
答案:
码上飞
码上飞(CodeFlying) 是一款AI自动化开发平台,通过自然语言描述即可自动生成完整应用程序。
449
查看详情
首先,使用子查询找出 return_code 为 success 且 count(*) 大于或等于 2 的 refund_id。
select refund_id from 表 where return_code = 'success' group by refund_id h*ing count(*) >= 2
然后,使用这些 refund_id 来过滤主查询,选择满足条件的记录。
select * from 表
where refund_id in (
select refund_id from 表
where return_code = 'success'
group by refund_id
h*ing count(*) >= 2
);例如,对于给定的数据,这个查询将返回以下结果:
| 序号 | refund_id | return_code | |---|---|---| | 1 | 520403 | SUCCESS | | 2 | 520403 | SUCCESS |
以上就是如何查询存在特定值且出现两次的记录?的详细内容,更多请关注其它相关文章!
