从相关表中的条件表中删除行

时间:2014-03-17 14:55:21

标签: mysql

我有两张桌子:商店和公司。我希望能够根据公司的信息删除商店。 最初我尝试过:

DELETE FROM Store WHERE (SELECT * FROM Company WHERE companyValueA > 5).company_id = store.company_id AND store.wf = 0;

但这不起作用。我正在考虑做两者的连接并从连接中删除如下:

DELETE FROM (SELECT * FROM Store LEFT JOIN Company ON Store.company_id = Company.company_id) WHERE companyValueA > 5 AND wf = 0;

但这也不起作用。有谁知道我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

只是一个猜测......显然,在尝试之前做一个备份!!!

DELETE s 
  FROM store s
  JOIN company c
    ON c.company_id = s.company_id 
 WHERE c.companyValueA > 5
   AND s.wf = 0;

答案 1 :(得分:0)

如果您需要更兼容的声明,可以使用MYSQL's DELETE with JOIN syntax(我还没有使用它)或只收集要使用子查询删除的所有商店ID:

DELETE
  FROM store
 WHERE store_id IN (
                     SELECT S.store_id
                       FROM store S, company C
                      WHERE C.company_id = S.company_id
                        AND C.companyValueA > 5
                        AND S.wf = 0
                     )