SQL查询:我想查找某一表里是否有超过两次的记录如何查找

2025-03-23 01:59:47
推荐回答(3个)
回答1:

查询有2个以上X:
select * from 表 where X in
(
select X from 表
group by X
having count(X) > 1
)

查询X,Y,Z有2个以上:
select * from 表 where X in ( select X from 表 group by X having count(X) > 1)
union
select * from 表 where Y in ( select Y from 表 group by Y having count(Y) > 1)
union
select * from 表 where Z in ( select Zfrom 表 group by Z having count(Z) > 1)

回答2:

你的意思是多条记录X Y Z 的值一样?

比如这样

X  Y Z

1  2 3

1  2 3

1  1  1

1  1  1

select x,y,z,count(*) from tablename group by x,y,z having count(*)>1

回答3:

楼主的意思是要查找X、Y、Z都相同的记录吧?

select X,Y,Z from 表名 group by X,Y,Z having count(*)>1