我正在使用此查询打印出论坛板及其所有子论坛。正如可以预料的那样,会显示属于该论坛的所有帖子中的所有帖子。我想要发生的只是每个帖子的第一篇帖子与论坛标题一起显示。
查询:
SELECT tf_threads.*, tf_posts.* FROM tf_threads INNER JOIN tf_posts ON tf_threads.thread_id=tf_posts.thread_id AND tf_threads.parent_id=54 ORDER BY tf_posts.date ASC
请注意,parent_id
字段在真实查询中有一个变量。
因此。如果我有道理,任何人都可以帮我解决哪些查询只能从每个帖子中选择第一个帖子?
如果没有简单的(ish)答案,如果我在第二个表中使用了帖子编号字段,我怎么能这样做,例如,线程中的第一个帖子的编号为1,第二个帖子的编号为2,等等。如果我使用这种方法,我显然只想选择计数字段为1的帖子。我可以用AND post_number=1
扩展原始查询吗?
感谢阅读,
詹姆斯
答案 0 :(得分:2)
这样的东西?
编辑:我认为它必须是这样的,但我也不是SQL专家:
SELECT tf_threads.*, tf_posts_tmp.*
FROM tf_threads
LEFT JOIN (SELECT p1.*
FROM tf_posts as p1
LEFT JOIN tf_posts AS p2
ON p1.postid = p2.postid AND p1.date < p2.date) as tf_posts_tmp
ON (tf_threads.thread_id=tf_posts_tmp.thread_id)