--我上面写的是一个表中啊? 你的意思是查询有循环关系的吧? --不过,既然是循环关系,我上面的会把4条都查出来,改一下:--测试数据 create table tb(a int,b int) insert tb select 1021,1022 union all select 1022,1021 union all select 1023,1004 union all select 1004,1023 go--查询 select * from tb a where exists(select * from tb where a=a.b and b=a.a and a>a.a) go--删除测试 drop table tb/*--结果a b ----------- ----------- 1021 1022 1004 1023(所影响的行数为 2 行) --*/
--因为是循环关系,而且由于sql中没有行号,所以如果要严格按顺序的话,还得有其他字段确定记录顺序 --例如: --测试数据 create table tb(id int identity,a int,b int) insert tb select 1021,1022 union all select 1022,1021 union all select 1023,1004 union all select 1004,1023 go--查询 select * from tb a where exists(select * from tb where a=a.b and b=a.a and id>a.id) go--删除测试 drop table tb/*--结果id a b ----------- ----------- ----------- 1 1021 1022 3 1023 1004(所影响的行数为 2 行) --*/
--不过,既然是循环关系,我上面的会把4条都查出来,改一下:--测试数据
create table tb(a int,b int)
insert tb select 1021,1022
union all select 1022,1021
union all select 1023,1004
union all select 1004,1023
go--查询
select * from tb a
where exists(select * from tb where a=a.b and b=a.a and a>a.a)
go--删除测试
drop table tb/*--结果a b
----------- -----------
1021 1022
1004 1023(所影响的行数为 2 行)
--*/
--例如:
--测试数据
create table tb(id int identity,a int,b int)
insert tb select 1021,1022
union all select 1022,1021
union all select 1023,1004
union all select 1004,1023
go--查询
select * from tb a
where exists(select * from tb where a=a.b and b=a.a and id>a.id)
go--删除测试
drop table tb/*--结果id a b
----------- ----------- -----------
1 1021 1022
3 1023 1004(所影响的行数为 2 行)
--*/