从mysql中的表中删除选定的行

时间:2014-09-24 04:51:23

标签: mysql sql

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

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

1 个答案:

答案 0 :(得分: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)