如何在PostgreSQL中执行此查询?
我在SQL中的尝试
SELECT stories.*
where
user.id = 1 AND
user.id = usersources.userid AND
usersources.id = source.id AND
source.id = story.id
AND (story.id = user.id) IS NOT IN actions table
文字说明
SELECT
stories
WHERE
they have a relationship to a source AND
sources has a story AND
there IS NOT a row in actions from user and story (aka already acted on story)
上下文
这是一个用户可以订阅许多新闻来源的应用 并刷新闻。一旦用户刷新消息他们就不应该 再次看到那个新闻故事。
视觉表格
答案 0 :(得分:0)
好的,我认为这会让你满意。
SELECT s.*
FROM stories s
INNER JOIN actions a ON s.id = a.storyid
INNER JOIN sources s1 ON s.id = s1.id
INNER JOIN usersources us ON s1.id = us.sourceid
INNER JOIN users u ON us.userid = u.id
WHERE u.id = 1
AND u.id != a.userid