1

我有一个嵌套的 SQL 查询:

SELECT DISTINCT(topic_id)
FROM bb_posts 
WHERE topic_id NOT IN ( 
  SELECT topic_id FROM bb_posts 
  WHERE poster_id = $user_id AND post_status = 0 ) 
ORDER BY post_time DESC

我的网络服务器运行不允许嵌套查询的 MySQL 4.0。一些 SQL 大师可以使用 JOIN 术语建议相同的查询吗?我试了又试……但我想不通。感觉很笨。

4

2 回答 2

2

假设 topic_id 不可为空,您可以执行以下操作:

SELECT DISTINCT(T1.topic_id)
FROM bb_posts As T1
    Left Join bb_posts As T2
        On T2.topic_id = T1.topic_id
            And T2.poster_id = $user_id
            And T2.post_status = 0
Where T2.topic_id Is Null
ORDER BY T1.post_time DESC
于 2010-11-11T21:24:36.550 回答
1

我不太确定您的要求是什么,但通过查看您的查询,我认为这应该足够了。

SELECT DISTINCT(topic_id) 
FROM bb_posts 
WHERE poster_id <> $user_id OR post_status <> 0 
ORDER BY post_time DESC
于 2010-11-11T21:26:07.877 回答