6

我有这个查询:

    SELECT DATE( a.created_at ) AS order_date, count( * ) as cnt_order
    FROM `sales_order_item` AS a
    WHERE MONTH( a.created_at ) = MONTH( now())-1
    GROUP BY order_date

这将返回类似这样的结果(仅快照,否则将每 31 天返回一次):

order_date  cnt_order 
2012-08-29  580
2012-08-30  839
2012-08-31  1075

我的完整查询是根据上述选择进行选择的:

SELECT order_date
    , MAX(cnt_order) AS highest_order
FROM (
        SELECT DATE (a.created_at) AS order_date
            , count(*) AS cnt_order
        FROM `sales_order_item` AS a
        WHERE MONTH(a.created_at) = MONTH(now()) - 1
        GROUP BY order_date
    ) AS tmax

但结果是:

order_date  highest_order
2012-08-01  1075

哪个日期错误,并且总是选择假设为 2012-08-31 的第一行日期。也许这是一个我不知道的简单错误。那么如何让日期正确指向 2012-08-31?任何帮助都会很棒。

4

2 回答 2

1

您可以尝试订购子查询结果集;就像是:

SELECT
    DATE (a.created_at) AS order_date,
    COUNT(*) AS cnt_order
FROM
    `sales_order_item` AS a
WHERE
    MONTH(a.created_at) = MONTH(now()) - 1
GROUP BY
    order_date
ORDER BY
    cnt_order DESC
于 2012-09-20T06:09:24.363 回答
0

您可以添加ORDER BY order_date DESC子查询。

于 2012-09-20T06:10:00.597 回答