在MySQL中使用子查询进行DELETE

时间:2012-06-06 16:32:27

标签: mysql subquery sql-delete

我想转换以下内容

SELECT * `members` m
WHERE NOT EXISTS
  (SELECT s.mid
   FROM   memberships s 
   WHERE  s.mid = m.id);

到等效的删除语句。但是,下面的代码不起作用:

DELETE FROM `members` m
WHERE NOT EXISTS
  (SELECT s.mid
   FROM   memberships s 
   WHERE  s.mid = m.id);

1 个答案:

答案 0 :(得分:2)

您可以使用多表DELETE语法在表之间执行外连接:

DELETE FROM members
USING members LEFT JOIN memberships ON memberships.mid = members.id
WHERE memberships.mid IS NULL