日期:2014-05-18 浏览次数:20423 次
create table tb(id int,parentid int) insert into tb values(1 ,0) insert into tb values(2 ,0) insert into tb values(3 ,0) insert into tb values(4 ,1) insert into tb values(5 ,2) go select * from tb t where exists(select 1 from tb m where t.parentid = m.id) or not exists(select 1 from tb n where t.id = n.parentid) drop table tb /* id parentid ----------- ----------- 3 0 4 1 5 2 (所影响的行数为 3 行) */
------解决方案--------------------
select * from test where id not in ( select t1.id from test t1 join test t2 on t1.id = t2.parentid )