报告两年中每月的比较表

时间:2019-06-29 19:01:04

标签: mysql

我正在研究一个小的PHP和Mysql项目,我想显示我过去两年的销售额,例如2018和2019。我如何显示一年中的所有月份? 这是我的SQL查询:

SELECT YEAR(b.creation_date) AS annee
 , MONTH(b.creation_date) AS mois
, SUM(price * quantity) AS total_ca 
  FROM invoicesitems a
  JOIN invoices b
 WHERE a.invoice_id = b.invoice_id 
    AND b.client = 6
GROUP BY YEAR(b.creation_date), MONTH(b.creation_date)
ORDER BY YEAR(b.creation_date), MONTH(b.creation_date)

这是我用英语写的结果:

--------------------------
year | month | total_ca
--------------------------
2018 | 6     | 1548.00
--------------------------
2019 | 6     | 143.000

如您所见,我只有第6个月(六月),如何显示从1到12的所有月份。

1 个答案:

答案 0 :(得分:0)

确保您具有其他月份和年份的发票值

您还同时使用了显式连接和隐式连接sintax,请尝试使用适当的显式ON子句

  SELECT YEAR(invoices.creation_date) AS annee
    , MONTH(invoices.creation_date) AS mois
    , SUM(price * quantity) AS total_ca 
  FROM invoicesitems 
  INNER JOIN invoices  ON  invoicesitems.invoice_id = invoices.invoice_id 
  WHERE invoices.client = 6
  GROUP BY YEAR(invoices.creation_date), MONTH(invoices.creation_date)
  ORDER BY YEAR(invoices.creation_date), MONTH(invoices.creation_date)