php - 按组排序,然后按组排序

标签 php mysql

我有一个 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/

相关文章:

javascript - 从下拉列表中选择不同类型的值打开不同类型的输入法?

php - 像stumbleupon这样的网站能打开google.com吗

php - MySQL 多对多。删除还是编辑?

mysql - 将 12gb MySQL 数据库导入 PHPmyAdmin

php - 将数据从 PostgreSQL 数据库移动到新的 MySQL 数据库的最简单方法是什么?

javascript - 按钮根本不起作用(Javascript)

php - 如何在提交表单后保留先前上传的图像而不选择要在codeigniter中上传的新图像

MySQL pid 结束(无法启动 mysql)

php - 使用复选框从表中获取多行并将它们添加到另一个表中,然后从上一个表中删除

java - 带有远程数据库的 Spring。如何最小化网络延迟开销?