A | B |
1 | 3 |
1 | 3 |
2 | 5 |
2 | 4 |
3 | 7 |
3 | 7 |
select * from T where T.A in (select Ti.A from T Ti group by Ti.A having max(Ti.B)<>min(Ti.B))
也可以采用另一种写法
select * from T where T.A in (select Ti.A from (select distinct A,B from T) Ti group by Ti.A having count(Ti.B)>1
上面的写法先去除A,B当中重复的列,然后根据A列分组,计算B列不同值的个数,此方法相比较上面一种增加了一个select,复杂了一些!
不够发现可以把内部的select 去掉,简化版如下:
select * from T where T.A in (select Ti.A from Ti group by Ti.A having count(distinct Ti.B)>1
原文:http://www.cnblogs.com/dyc0113/p/4159376.html