MYSQL连接查询建议(删除孤立行)

时间:2012-03-30 12:46:49

标签: mysql sql join

我有两张表如下..

campaigns(campaignID, title)
campaignMailList(campaignID, Sent)

(2个表格更大..但为此我只需要这些列)

我想生成一个查询,该查询将删除campaignMailList中的所有行,而该行无法在campaignID上的广告系列表中找到联接。

有人可以建议我这样做吗?

3 个答案:

答案 0 :(得分:2)

您可以尝试NOT IN子句。 像

DELETE FROM `campaignMailList` as cml
WHERE cml.campaignid NOT IN ( SELECT campaignID FROM campaigns )

不确定这是否是最好的方法

答案 1 :(得分:2)

将您自己的问题逐字翻译成SQL:

delete from campaingMailList where
campaingId not in (select campaingid from campaigns)

答案 2 :(得分:1)

尝试此查询 -

DELETE cml FROM campaignMailList cml
  LEFT JOIN campaigns c
    ON c.campaignID = cml.campaignID
WHERE c.campaignID IS NULL