日期:2014-05-17  浏览次数:20505 次

查找表中某两列值重复的行,跪求SQL语句
表名:tb_a

数据如下
name_id seq_id buy_id sell_id
5156 10 213 123
453 10 2528 85
453 20 2582 78
453 20 254 765
154 20 2528 45
45 30 45 45
45 30 452 45
45 30 45 45

以seq_id 分组,group by seq_id,想找出 name_id,seq_id这组数据相同的行如下面

name_id seq_id buy_id sell_id
453 20 2582 78
453 20 254 765

这第一行 (453,20) = (453,20)第二行 


从上面例子需要显示的表格

name_id seq_id buy_id sell_id
453 20 2582 78
453 20 254 765
45 30 45 45
45 30 452 45
45 30 42 45


跪求sql语句,非常感谢!

------解决方案--------------------
select * from tb_a a
where exists (
select 1
from tb_a b
where b.name_id = a.name_id
and b.seq_id = a.seq_id
and (a.buy_id<> b.buy_id
or a.sell_id<> b.sell_id)
)

------解决方案--------------------
考虑buy_id sell_id可能null,提供另一个写法

select a.* from tb_a a
,(select name_id,seq_id
from tb_a
group by name_id,seq_id
having count(1) > 1
) as b
where a.name_id=b.name_id
and a.seq_id=b.seq_id