我有两张桌子 - forum_topics
和topics_posts
。我想选择forum_topics
中topics_posts
表中没有帖子的行,但无法弄清楚如何。是否存在这样的SQL语句:
select from * `forum_topics` where have no rows in `topics_posts`
答案 0 :(得分:2)
我想你想要这样的东西:
select * from forum_topics t
where not exists (
select * from topics_posts p
where p.topic_id = t.id
);
虽然使用外连接,但可能比子查询快一点:
select * from forum_topics t left outer join forum_posts p
on t.id = p.topic_id
where p.id is null;