我有以下MySQL。
SELECT
`outputtable`.`date`,
count(*) as `count`
FROM (
SELECT
CONCAT(DATE(`mytable`.`starttime`),' ',HOUR(`mytable`.`starttime`),':',LPAD(10*(MINUTE(`mytable`.`starttime`) DIV 10),2,'0')) as `date`,
`mytable`.`clientid`
FROM
`mytable`
WHERE
`mytable`.`clientid`='1'
GROUP BY
`mytable`.`clientid`
ORDER BY
`date`
) AS outputtable
GROUP BY
`date`
ORDER BY
`date` ASC
输出的日期字段没有按照日期时间排序规则正确排序。
输出排序示例:
2011-02-01 17:00 | 4
2011-02-01 18:00 | 1
2011-02-01 19:00 | 1
2011-02-01 21:00 | 1
2011-02-01 8:00 | 6
2011-02-01 9:00 | 7
我认为这是因为新创建的名为"date"的字段是varchar。
如何将表"outputable"中字段"date"的类型设置为"Datetime",以使其正确排序?
提前感谢
H。
只需使用Datetime作为列类型:D
将其转换为最新版本,如下所示:
cast(CONCAT(DATE(`mytable`.`starttime`),' ',HOUR(`mytable`.`starttime`),':',LPAD(10*(MINUTE(`mytable`.`starttime`) DIV 10),2,'0')) as DATE) as date
或更可读:
SELECT
cast(`outputtable`.`date` as date),
count(*) as `count`
-- the rest of the query the same