2012-11-06 51 views
3

表1:从表中删除其他表中找不到键的位置?

  • ID
  • 名称

表2:

  • ID
  • other_table_id
  • table_1_id
  • ..

基本上就是我想要做的就是

Delete from table_1 
where id not in (select table_1_id 
       from table_2 
       group by table_1_id); 

这应该工作,我想知道是如果子查询做到这一点的最好办法/是有没有其他办法?

回答

8

我宁愿使用JOIN而不是子查询

DELETE a FROM table_a a 
      LEFT JOIN table_2 b 
       ON a.ID = b.table_1_id 
WHERE b.table_1_id IS NULL 
+1

需要预订标记SQLFiddle。总是忘记它。 –

相关问题