我有一个 phpbb mysql 表,它创建了一行包含 post_id topic_id 和 forum_id 列。
对于每个帖子,一行的数据是:
post_id topic_id forum_id
2 1 12
1 1 12
3 2 12
4 2 12
6 3 12
5 3 12
2 1 13
1 1 13
3 2 13
4 2 13
6 3 13
5 3 13
我只想获取每个 topic_id 的最小 post_id 的行,以获得如下内容:
post_id topic_id forum_id
1 1 12
3 2 12
5 3 12
1 1 13
3 2 13
5 3 13
有人可以帮助我吗? (我考虑使用 order by 然后 group by...但并不像我想象的那样工作...
最佳答案
尝试这样的事情......
select post_id, topic_id, forum_id
from thetable t
where
(post_id,topic_id) = (select min(t1.post_id), t1.topic_id
from thetable t1
where t1.topicid = t.topicid group by t1.topic_id)
这表示从表中获取子选择模式中 post_id 和 topic_id 的所有记录。
关于php - 按组排序,然后按组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427748/