1

发出此 sql 请求后:

SELECT user.id, campaign_id, user_id
FROM `user`
LEFT OUTER JOIN `email_solus`
ON email_solus.user_id=user.id

我有这张桌子。

id campaign_id user_id
------ ----------- ---------
 1   3         1
 1   5         1
 1   6         1
 2 (NULL) (NULL)
 3 (NULL) (NULL)
 4 (NULL) (NULL)
 5 (NULL) (NULL)
 7 (NULL) (NULL)
 8 (NULL) (NULL)
 9 (NULL) (NULL)
10 (NULL) (NULL)
12 (NULL) (NULL)
13 (NULL) (NULL)
14 (NULL) (NULL)
15 (NULL) (NULL)
16 (NULL) (NULL)
17 (NULL) (NULL)

我想做的是取消选择所有带有campaign_id = 3的元素,以及所有常见的ID。在这种情况下,我需要删除所有 id=1 的元素,有一个像这样的表:

id campaign_id user_id
------ ----------- ---------
 2 (NULL) (NULL)
 3 (NULL) (NULL)
 4 (NULL) (NULL)
 5 (NULL) (NULL)
 7 (NULL) (NULL)
 8 (NULL) (NULL)
 9 (NULL) (NULL)
10 (NULL) (NULL)
12 (NULL) (NULL)
13 (NULL) (NULL)
14 (NULL) (NULL)
15 (NULL) (NULL)
16 (NULL) (NULL)
17 (NULL) (NULL)
4

3 回答 3

3
SELECT user.id, campaign_id, user_id
FROM user
LEFT JOIN email_solus
ON email_solus.user_id=user.id
WHERE user.id NOT IN
(
    SELECT user_id
    FROM email_solus
    WHERE campaign_id = 3
)
于 2012-11-08T15:06:01.400 回答
2

您可以使用NOT EXISTS进行过滤,如下所示:

SELECT user.id, campaign_id, user_id
FROM `user`
LEFT OUTER JOIN `email_solus` ON email_solus.user_id=user.id
WHERE NOT EXISTS (
    SELECT * FROM `email_solus` es WHERE es.user_id=user_id AND es.campaign_id=3
)
于 2012-11-08T15:07:26.437 回答
0
SELECT user.id, campaign_id, user_id
FROM `user`
LEFT OUTER JOIN `email_solus`
ON email_solus.user_id=user.id
where user.id not in
  (select user.id from 'user'
  where campaign_id = 3)

应该可以工作,尽管如果你有一个大型数据集,“不在”可能效率低下

于 2012-11-08T15:07:07.423 回答