SQL 排序月份,为什么我在 1 月之后得到 12 月?
SQL sorting months, why am I getting December right after January?
我有这个查询,几乎完美无缺:
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
输出是年份,日期格式如下:
month:id,id,id-month:id,id,id-etc..
我的问题是,在结果上,我的 12 月出现在 1 月之后。
正如您在第二行看到的那样,我有 1:128、12:138 等,因此紧接着是 1(一月)和 2(十二月)。为什么?
目前我的解决方案是将 ORDER BY tbl.m
添加到第一行:
SELECT tbl.y year, group_concat(month_posts ORDER BY tbl.m SEPARATOR '-') dates
我有这个查询,几乎完美无缺:
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
输出是年份,日期格式如下:
month:id,id,id-month:id,id,id-etc..
我的问题是,在结果上,我的 12 月出现在 1 月之后。
正如您在第二行看到的那样,我有 1:128、12:138 等,因此紧接着是 1(一月)和 2(十二月)。为什么?
目前我的解决方案是将 ORDER BY tbl.m
添加到第一行:
SELECT tbl.y year, group_concat(month_posts ORDER BY tbl.m SEPARATOR '-') dates