1

我想在我的 xhtml 页面中绘制一个线性图表。横坐标是月份,纵坐标是每月的订单数量。在您看来,是否可以创建一个 mysql 查询来返回一个列表,其中每一行在第二个属性中包含一个月和订单数量。

这是我的桌子:

在此处输入图像描述

这是检索所有 date_envoi 的 sql 查询:

select date_envoi from commande;

这是图像:

在此处输入图像描述

4

1 回答 1

2

您需要GROUP通过MONTHand记录YEAR

SELECT DATE_FORMAT(date_envoi, '%M %Y') AS month,
       COUNT(*) AS order_quantity
FROM commande
GROUP BY YEAR(date_envoi), MONTH(date_envoi);

编辑:以数字格式获取月份:

SELECT YEAR(date_envoi) AS year,
       MONTH(date_envoi) AS month,
       COUNT(*) AS order_quantity
FROM commande
GROUP BY YEAR(date_envoi), MONTH(date_envoi);

编辑2:

SELECT `month`, SUM(order_quantity) AS order_quantity
FROM(

    SELECT MONTH(date_envoi) AS month,
           COUNT(*) AS order_quantity
    FROM commande
    GROUP BY MONTH(date_envoi)
    UNION ALL
    SELECT 1 AS month, 0 order_quantity
    UNION ALL
    SELECT 2 AS month, 0 order_quantity
    UNION ALL
    SELECT 3 AS month, 0 order_quantity
    UNION ALL
    SELECT 4 AS month, 0 order_quantity
    UNION ALL
    SELECT 5 AS month, 0 order_quantity
    UNION ALL
    SELECT 6 AS month, 0 order_quantity
    UNION ALL
    SELECT 7 AS month, 0 order_quantity
    UNION ALL
    SELECT 8 AS month, 0 order_quantity
    UNION ALL
    SELECT 9 AS month, 0 order_quantity
    UNION ALL
    SELECT 10 AS month, 0 order_quantity
    UNION ALL
    SELECT 11 AS month, 0 order_quantity
    UNION ALL
    SELECT 12 AS month, 0 order_quantity
) a
GROUP BY `month`
ORDER BY `month` ASC;
于 2012-09-07T10:12:16.460 回答