您的位置:首頁技術文章
文章詳情頁

mysql group by 不能排序問題

瀏覽:112日期:2022-06-13 13:56:54

問題描述

我現在有一個文章表 有兩個字段 id 和 catid我想給 每個catid 有8個分類 列出 最新發的一篇文章

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

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

如何才能按照id號desc開始排序

問題解答

回答1:

你select * 本來就是不嚴謹的寫法,只是mysql兼容性比較強默認給你放最小的,如果sql帶有group by,那么select里頭只能包含group by中的列或者是聚合函數,你評論說的沒法order by 是因為需要對max后的列加一個別名。

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

mysql group by 不能排序問題

mysql group by 不能排序問題

回答2:

select * from post gruop by catid order by id desc;回答3:

gruop by catid 寫在后面

相關文章:
国产综合久久一区二区三区