SQL语句查询重复记录问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 08:07:44
RT:
Col1 Col2
1111 aaaa
1111 aaaa
2222 aaaa
2222 bbbb
3333 cccc
现在我想筛选出Col2重复但Col1不重复的记录,请问Sql语句怎么写

我这个才是正确的,O(∩_∩)O
1111 aaaa
1111 aaaa
2222 aaaa
2222 bbbb
3333 cccc
4444 cccc

select * From test1 where col2 in (select col2 from test1 group by col2 having count(*)>=2)

结果:
1111 aaaa
1111 aaaa
2222 aaaa
3333 cccc
4444 cccc
根据col2重复的行进行筛选。
having子句最重要!
另外,数据表不要包含相同的行

用having 关键字,具体怎么写忘了,百度一下。

select distinct(Col2) from 这个表的名字

select * from t
where col2 = any (
select col2 from t group by col2 having count(col1) > 1
)
group by col1 having count(col2) = 1

distinct好像是这个关键字。SQL语句加上这个字就行了。

select * From test1 where col2 in (select col2 from test1 group by col2 having count(*)>=2) and col1 in (select col1 from test1 group by col1,col2 having count(*)=1)

运行结果
2222 aaaa