我试图获取仅对某些用户可见的帖子的ID列表。我目前的陈述是:
SELECT `p`.`id`
FROM `posts` AS `p`
INNER JOIN `post_visibilities` AS `pv`
ON `pv`.`post_id` = `p`.`id`
WHERE `pv`.`user_id` IN ( 1, 2, 3, 4 );
这有点奏效,但IN()
子句的行为有点像子集,我收到的帖子已被这四个用户和其他用户看到了。我需要找到仅这四个用户可见的帖子。
答案 0 :(得分:0)
您可以直接翻译并使用不存在的内容。 虽然我不得不说一个不存在(因此一个子查询)总是有点慢。 不确定是否有更快的方法。
SELECT DISTINCT `p`.`id`
FROM `posts` AS `p`
INNER JOIN `post_visibilities` AS `pv`
ON `pv`.`post_id` = `p`.`id`
WHERE `pv`.`user_id` IN ( 1, 2, 3, 4 )
and not exists (select 1 from `post_visibilities` as 'excluder'
where excluder.post_id = p.id and excluder.user_id not in (1,2,3,4));
答案 1 :(得分:0)
使用条件SUM
post_visibilty
每个帖子只有ID
一次。ID
的所有4个post_visibilty
都有SUM
将为4 post_visibilty
SUM
将永远不会是4。
SELECT `p`.`id`
FROM `posts` AS `p`
INNER JOIN `post_visibilities` AS `pv`
ON `pv`.`post_id` = `p`.`id`
GROUP BY `p`.`id`
HAVING SUM (CASE
WHEN `pv`.`user_id` IN ( 1, 2, 3, 4 ) THEN 1
ELSE -1
END) = 4;
您的问题的其他解释也可以使用条件SUM
HAVING
SUM (CASE
WHEN `pv`.`user_id` IN ( 1, 2, 3, 4 ) THEN 1
ELSE 0
END) >= 1 -- one or more have visibilty
AND SUM (CASE
WHEN `pv`.`user_id` NOT IN ( 1, 2, 3, 4 ) THEN 1
ELSE 0
END) = 0 -- no one else have visibilty