通过子查询按组排序不起作用

时间:2012-06-11 09:39:51

标签: mysql group-by sql-order-by subquery

我在mysql中有一个简单的统计信息查询。 我有它使用PHP工作但是热衷于将它保存在数据库中,所以我在这里找到了另一个问题。多亏了我现在让它在mysql中工作。

我需要获得每天提交的记录的总计数。

我有正在运行的总计,但无论我改变什么,它都不会在白天订购。

它将按正在运行的总列排序,但不会按当天排序。

赞赏任何意见。

SET @runtot:=0;
SELECT
   q1.`Day` AS 'Days of month',
   q1.`Record count` AS 'Record count',
   (@runtot := @runtot +  q1.`Record count`) AS 'rt'
FROM
   (SELECT
    FROM_UNIXTIME(tr.`tr_last_update`, '%e') AS 'Day',
    COUNT(tr.tr_id_pk) AS 'Record Count'
    FROM records_table AS tr
    GROUP BY FROM_UNIXTIME(tr.`tr_last_update`,'%e')
) AS q1
ORDER BY 'Days of month';

Days of month   Record count    rt
10                  13          13
11                  2           15
7                   255         270
8                   173         443
9                   166         609

2 个答案:

答案 0 :(得分:0)

它被视为一个字符串。尝试转换为int:

SELECT
q1.`Day` AS 'Days of month',
...
ORDER BY CAST(q1.`Day` AS signed integer)

答案 1 :(得分:0)

除非我错了,否则你的整个查询不一样:

SELECT   DAYOFMONTH(FROM_UNIXTIME(tr_last_update)) AS `Days of month`,
         COUNT(tr_id_pk)                           AS `Record Count`,
         @runtot := @runtot + COUNT(tr_id_pk)      AS rt
FROM     records_table, (SELECT @runtot:=0) AS z
GROUP BY `Days of month`
ORDER BY `Days of month`