gpt4 book ai didi

mysql - SQL 按月份名称按时间顺序而不是按字母顺序排序

转载 作者:行者123 更新时间:2023-11-29 10:47:39 27 4
gpt4 key购买 nike

我想让月份名称按时间顺序排列,而不是按字母顺序排列。这是我的 Sql 代码。

SELECT month, sum(total) 
FROM (SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) AS total
FROM projects
WHERE terms >= '2017/01/01'
GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) AS total
FROM archive
WHERE terms >= '2017/01/01'
GROUP BY MONTH(terms)
) AS test
GROUP BY month
ORDER BY month

上面代码的输出看起来像 This

我希望它是:

January

February

March

...

...

最佳答案

将月份作为 int 进行排序。 使用 MONTH(STR_TO_DATE(month, '%M'))

SELECT month, sum(total) 
FROM (SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) AS total
FROM projects
WHERE terms >= '2017/01/01'
GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) AS total
FROM archive
WHERE terms >= '2017/01/01'
GROUP BY MONTH(terms)
) AS test
GROUP BY month
ORDER BY MONTH(STR_TO_DATE(month, '%M'))

关于mysql - SQL 按月份名称按时间顺序而不是按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44286320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com