如何按日名称在mysql中订购,即:周日,周一,周二,周三等等
SELECT
DAYNAME(dl.bookingDate) AS dayName, COUNT(dg.idDiary) AS totalBooked
FROM
diary_list dl, diary_group dg
WHERE
dl.idDiary = dg.idDiary AND dg.idService != 0 AND dl.status = 3
GROUP BY
dayName
ORDER BY
dayName ???
答案 0 :(得分:7)
您需要将功能DAYOFWEEK
添加到GROUP BY
和ORDER BY
:
GROUP BY DAYOFWEEK(dl.bookingDate)
ORDER BY DAYOFWEEK(dl.bookingDate)
您可能还希望使用显式ANSI标准JOIN
语法。
SELECT
DAYNAME(dl.bookingDate) AS dayName, COUNT(dg.idDiary) AS totalBooked
FROM
diary_list dl
JOIN diary_group dg
ON dl.idDiary = dg.idDiary
WHERE
dg.idService != 0 AND dl.status = 3
GROUP BY
DAYOFWEEK(dl.bookingDate)
ORDER BY
DAYOFWEEK(dl.bookingDate)
答案 1 :(得分:2)
您可以制定自己的订单标准:
ORDER BY FIELD(DAY , '3', '4', '5', '6', '7','1', '2')
在你的情况下:
ORDER BY FIELD(DAY , '7','1', '2', '3', '4', '5', '6')
阅读此article了解更多信息