Postgres:选择左连接记录的记录,所有记录都不符合某些标准

时间:2015-09-04 09:51:20

标签: postgresql

例如,我有一个帖子表和一个用户表。

用户有:post_id,:user_id,:published_date,:body

我想要做的是选择从未在任何帖子上设置published_date的用户。

我正在考虑从用户开始然后离开加入帖子表但我不知道在我的where子句上放什么,因为它需要选择所有帖子都没有发布日期的用户

2 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

bool_and

  

select user_id from t group by user_id having bool_and(published_date is null) - bool_and(expression)如果所有输入值均为true,则为true

答案 1 :(得分:-1)

如下:

SELECT user.user_id
FROM user
LEFT JOIN post ON post.user_id=user.user_id AND post.published_date IS NOT NULL
WHERE post.post_id IS NULL

您将每个用户与其发布日期的帖子进行匹配。如果没有,则左连接将为post表中的匹配返回空值(即使对于永远不应为NULL的列,例如post_id主键)。您在WHERE子句中使用它只返回没有匹配的行。

相关问题