我想选择未标记为用户阅读的所有帖子。
tbl_post
post_id post_message
1 hello world
2 good night
3 good morning
4 incredible
5 cool
tbl_mark_as_read
user_id post_id
3 1
3 4
我想选择在此查询中未选择的所有数据
SELECT p.post_id,p.post_message FROM tbl_post AS p
LEFT JOIN tbl_mark_as_read AS r
ON r.post_id = p.post_id
AND r.user_id = 3
我希望输出像
post_id post_message
2 good night
3 good morning
5 cool
答案 0 :(得分:3)
您可以使用not in
运算符:
SELECT *
FROM tbl_post
WHERE post_in NOT IN (SELECT post_id FROM tbl_mark_as_read);
如果您只想排除用户3已阅读的帖子,您可以在内部查询中添加where
子句:
SELECT *
FROM tbl_post
WHERE post_in NOT IN (SELECT post_id
FROM tbl_mark_as_read
WHERE user_id = 3);
答案 1 :(得分:2)
试试这个:
SELECT p.post_id,p.post_message FROM tbl_post AS p
LEFT JOIN tbl_mark_as_read AS r
ON r.post_id = p.post_id
AND r.user_id = 3
where r is null