Home  >  Q&A  >  body text

mysql group by 不能排序问题

我现在有一个文章表 有两个字段 id 和 catid
我想给 每个catid 有8个分类 列出 最新发的一篇文章

select * from post gruop by catid order by id desc;

这样我只能取出 8条 id 最小的 也就是 最老的一篇文章 order by id 并不起作用

如何才能按照id号desc开始排序

PHPzPHPz2742 days ago1085

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 16:54:09

    Your select * is originally not a rigorous way of writing, but MySQL has strong compatibility and defaults to the smallest one. If SQL has group by, then the select can only contain columns in group by or aggregation functions , you commented that order by cannot be done because you need to add an alias to the column after max.

    select max(id) id, catid from post group by catid order by id desc;

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:54:09

    select * from post gruop by catid order by id desc;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:54:09

    gruop by catid written at the back

    reply
    0
  • Cancelreply