gpt4 book ai didi

mysql - 从一对多mysql关系中选择有序且唯一的值

转载 作者:行者123 更新时间:2023-11-29 22:58:57 24 4
gpt4 key购买 nike

我想选择最新 10 个活跃主题。即最后有帖子的主题。在 PHPbb3 架构中,表 topics 有许多 post。为了实现这一点,我尝试了以下SQL

    SELECT DISTINCT (`t_topics`.`topic_id`), `t_topics`.topic_title   FROM 
`t_topics` , `t_posts` WHERE `t_posts`.topic_id = `t_topics`.topic_id
ORDER BY `t_posts`.post_id DESC LIMIT 10;

但是,有一个主题,我确信它有最新的帖子,并且位于记录的末尾。

我尝试删除 DISTINCT 但是,我得到了正确的顺序,但有重复的主题。我想获得没有重复主题的正确顺序,但我不知道如何?

最佳答案

您可以在 t_topics.topic_id 上使用 group by 子句而不是 Distinct。查询是: SELECT t_topics.topic_id, t_topics.topic_title FROM t_topics , t_posts WHERE t_posts.topic_id = t_topics.topic_id GROUP BY t_topics.topic_id ORDER BY t_posts.post_id DESC 限制 10;

关于mysql - 从一对多mysql关系中选择有序且唯一的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com