日期:2014-05-17 浏览次数:20437 次
create table 表
(c1 int,c2 varchar(10))
insert into 表
select 1,'a' union all
select 2,'b' union all
select 3,'a' union all
select 4,'c' union all
select 5,'b' union all
select 6,'d'
select c1,c2
from 表 a
where exists(select 1 from 表 b
where b.c1<>a.c1 and b.c2=a.c2)
order by c2,c1
/*
c1 c2
----------- ----------
1 a
3 a
2 b
5 b
(4 row(s) affected)
*/
;with testtb(c1,c2)
as
(
select 1,'a'
union all
select 2,'b'
union all
select 3,'a'
union all
select 4,'c'
union all
select 5,'b'
union all
select 6,'d'
)
select * from testtb
order by c2,c1