2010-03-17 35 views
0

我有两个表 - forum_topicstopics_posts。我想从forum_topics中选择topics_posts表中没有帖子的行,但无法弄清楚如何操作。请问一个SQL语句像这样存在:MySQL选择行不在另一个表中的行

select from * `forum_topics` where have no rows in `topics_posts` 

回答

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; 
+0

非常感谢你的工作好(外连接) – 2010-03-17 10:02:11

相关问题