2014-09-24 146 views
2

从表中删除此选择的结果的最佳方法是什么?从mysql表中删除选定的行

select id from taxon2 as a 
    where rank = 'No Taxon' and 
    (select count(*) from taxon2 as b 
     where a.id = b.parentid) = 0; 
+0

的可能重复[如何从选择MySQL中删除?](http://stackoverflow.com/questions/4562787/how-to-delete-from-select-in-mysql) – 2014-09-24 04:58:29

+0

不,那个答案在这里不适用(或工作)。 a。和b。在选择时需要不同的答案。 – xpda 2014-09-24 05:02:59

回答

4

这里是具有OUTER JOIN的溶液:

delete taxon2 
from taxon2 
    left join taxon2 t2 on taxon2.id = t2.parentid 
where t2.id is null; 

并与NOT EXISTS

delete from taxon2 
where rank = 'No Taxon' 
    and not exists (
     select 1 
     from (select * from taxon2) as b 
     where b.parentid=taxon2.id) 
+0

工作 - 谢谢! – xpda 2014-09-24 05:08:13

+0

@xpda - 很高兴能够提供帮助。现在我看到了你的'rdbms',我也包含了'outer join'解决方案。 – sgeddes 2014-09-24 05:11:23