首页 > 解决方案 > group by 和 order by,不工作 mysql

问题描述

我想订购一个帖子列表,id_post并根据活跃或过时对其进行分组。status=1 when active,status=2 when outdated

SELECT *,count(id_post) as count
FROM posts
WHERE 1

GROUP BY status
ORDER BY count DESC 

我尝试使用这个 mysql 代码,但它只显示两个输出。我究竟做错了什么??

id_post title status
  5        a     1
  7        b     2
  9        c     1
 17        d     2
 21        f     1

预期产出

21 f 1
9  c 1
5  a 1
17 d 2
7  b 2

标签: mysql

解决方案


根据您的数据,您不需要使用GROUP BY. 所以

select *
from posts
order by status ASC, id_post DESC

推荐阅读