单个查询SQL中的多个计数

时间:2019-06-05 19:54:32

标签: mysql

我编写了此SQL代码,以连续数月计算不同的值。

"SELECT 
            (SELECT COUNT(id) MONTH(date) AS inMonth, YEAR(date) AS inYear  FROM ptiseis WHERE katigoria='prosarmogi') low,
            (SELECT COUNT(id) MONTH(date) AS inMonth, YEAR(date) AS inYear  FROM ptiseis WHERE  katigoria='organa') moderate,
            (SELECT COUNT(id) MONTH(date) AS inMonth, YEAR(date) AS inYear  FROM ptiseis WHERE  katigoria='nautilia') high,

          GROUP BY MONTH(date), YEAR(date)
          ORDER BY inYear, inMonth  ";

但是它不起作用。我有什么问题以及如何解决?

1 个答案:

答案 0 :(得分:2)

我认为这是您想要做的:

SELECT 
  YEAR(date) AS inYear,
  MONTH(date) AS inMonth,
  SUM(katigoria='prosarmogi') low,
  SUM(katigoria='organa') moderate,
  SUM(katigoria='nautilia') high
FROM ptiseis
GROUP BY inYear, inMonth
ORDER BY inYear, inMonth;

您要计算按年,月分组的3种情况中的每种情况的行数。