2016-04-01 62 views

回答

1

最簡單的方法是做使​​用NOT EXISTS返回在列parent_id沒有發現這些ID的:的

select id 
from tablename t1 
where not exists (select 1 from tablename t2 
        where t2.parent_id = t1.id) 

NOT IN也可使用,但要小心,NULL的都被照顧:

select id 
from tablename 
where id not in (select parent_id from tablename 
       where parent_id is not null) 
相關問題