我在MySQL数据库上使用的SQL是
SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM
TABLE
WHERE
EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY
WEEKDAY_DATE
ORDER BY WEEKDAY_DATE
它生成的周名输出格式为“YYYY-MON-DAY”。目前,输出未订购。我想订购如下
2015-01-Sun
2015-01-Mon
2015-01-Tue
2015-01-Wed
2015-01-Thu
2015-01-Fri
2015-01-Sat
2025-02-Sun
2015-02-Mon
2015-02-Tue
2015-02-Wed
2015-02-Thu
2015-02-Fri
2015-02-Sat
and so on
有人可以帮助我吗?
答案 0 :(得分:1)
您可以按a
订购:
EVE_DATE
的 SqlFiddleDemo
强>
编辑:
我想在评论中传达的例子:
SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM TABLE
WHERE EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY WEEKDAY_DATE
ORDER BY EVE_DATE
按日期排序时,按实际日期排序,而不是文字表示。
现在您想从 2015-01-01 - Thursday -- 4th
2015-01-02 - Friday -- 5th
2015-01-03 - Saturday -- 6th
2015-01-04 - Sunday -- 7th
2015-01-05 - Monay -- should it be the first
2015-01-06 - Tuesday -- 2nd
2015-01-07 - Wednesday -- 3rd
2015-01-08 - Thurday
2015-01-09 - Friday
2015-01-10 - Saturday
2015-01-11 - Sunday
2015-01-12 - Monday -- 8th
开始,但是今年/月的周一不存在作为起点。你想洗牌Monady吗?你的建议对我来说是无稽之谈。
答案 1 :(得分:1)
当MySQL有concat()
时,为什么要使用date_format()
语句?
SELECT DATE_FORMAT(eve_date, '%Y-%m-%a') as weekday_date,
SUM(EVE_OCCUR)
FROM TABLE
WHERE eve_date >= '2015-01-01' AND eve_date < '2015-02-01'
GROUP BY WEEKDAY_DATE
ORDER BY MIN(eve_date);
对于ORDER BY
,我建议使用聚合函数,而不是ORDER BY eve_date
。这与标准SQL的工作方式一致。
我还将日期比较更改为使用>=
和<
。 BETWEEN
是一个带日期的危险习惯,因为当有时间组件时,它的工作方式不同。上述方法同样适用于日期和日期/时间。
答案 2 :(得分:0)
您可以使用EVE_DATE列对Query
中的输出进行排序SELECT
CONCAT(YEAR(EVE_DATE),'-',MONTH(EVE_DATE),'-',DAYNAME(EVE_DATE)) AS WEEKDAY_DATE,
SUM(EVE_OCCUR)
FROM
TABLE
WHERE
EVE_DATE BETWEEN '2015-01-01' AND '2015-10-31'
GROUP BY
WEEKDAY_DATE
ORDER BY YEAR(EVE_DATE),MONTH(EVE_DATE) ,FIELD(DAYNAME(EVE_DATE), 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');