我正在处理下面的代码:
SELECT tbl.y YEAR, group_concat(month_posts SEPARATOR '-') dates
FROM (
SELECT YEAR(p.post_date) y, MONTH(p.post_date) m, concat(MONTH(p.post_date), ':', group_concat(p.id ORDER BY p.post_date ASC)) month_posts
FROM prt_posts p
WHERE (p.post_status = 'publish' OR p.post_status = 'future')
AND p.post_type = 'event'
AND p.post_date <= DATE('2016-12-31 00:00:00')
GROUP BY y, m
ORDER BY y, m ASC
) tbl
GROUP BY tbl.y
ORDER BY tbl.y DESC
结果是这样的:
year | dates
-------------------------------------------------------------------
2016 | 11:121,122-12:117,118
-------------------------------------------------------------------
2015 | 1:128-12:138-2:139-3:129-4:130-5:131-6:132-7:133-8:119-9:134
-------------------------------------------------------------------
除了日期顺序之外,一切都很好。正如你在第二行看到的,它是 1, 12, 2, 3, 等等...而它应该是 1, 2, 3, 4, 等等.. 它以字符串而不是数字的形式排序。
如何解决?
最佳答案
已经找到答案:我可以通过将第一行更改为这样进行排序:
SELECT tbl.y YEAR, group_concat(month_posts **ORDER BY tbl.m DESC** SEPARATOR '-') dates
关于mysql - 如何正确按月份对sql查询结果排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261703/