MYSQL查询中GROUP_CONCAT的替代方案

时间:2013-11-18 03:44:52

标签: mysql sql filemaker group-concat

有人可以告诉我如何更改我的查询,以便它不会使用GROUP_CONCAT。 该查询适用于MYSQL和PHP,但我需要在Filemaker Pro中运行相同的查询,但Filemaker不支持GROUP CONCAT。

SELECT sqlvalue
CAST(GROUP_CONCAT(IF(stat_number = 693, sqlvalue, NULL)) AS UNSIGNED) AS S693,
CAST(GROUP_CONCAT(IF(stat_number = 698, sqlvalue, NULL)) AS UNSIGNED) AS S698,
CAST(GROUP_CONCAT(IF(stat_number = 2010, sqlvalue, NULL)) AS UNSIGNED) AS S2010,
CAST(GROUP_CONCAT(IF(stat_number = 2020, sqlvalue, NULL)) AS UNSIGNED) AS S2020,
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 693, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 698, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 2010, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 2020, sqlvalue, NULL)) AS UNSIGNED),0) + AS STOTAL
FROM statsvalue
WHERE sqldate > ’01-07-2013′
ORDER BY sqldate ASC
GROUP By sqldate

字段stat_number是为输入的记录提供的代码,sqlvalue是数字值,sqldate是日期。

上述查询会获取stat_number的值并将它们相加,并将总数按sqldate field分组。

1 个答案:

答案 0 :(得分:2)

也许我错过了一些东西,但在这种特殊情况下你不需要GROUP_CONCAT()(做条件聚合)。

因此恕我直言,您可以继续使用GROUP_CONCAT()更改MAX(),这可能更有意义,至少在阅读查询时更有意义。

我不知道Filemaker,但您也可以考虑更改

  1. IF()CASE END
  2. IFNULL()COALESCE()
  3. 除此之外,您的查询还有其他一些问题:

    1. 即使MySQL允许您不应该在sqlvalue子句中使用SELECT列,而没有将聚合函数应用于它作为group by的一部分。它只是没有意义,其他主要的RDBMS(SQL Server,Oracle,Postgres)不允许它。 您最有可能打算使用sqldate
    2. 即使您将sqlvalue放在SELECT中,也应该在列名后面使用逗号
    3. +
    4. 之前还有一个额外的AS STOTAL
    5. 使用日期字符串文字时,您可能需要使用正确的引号而不是智能引号
    6. 您最好使用消除歧义的日期文字(而不是'01 -07-2013',您应该使用'2013-07-01'或'2013-01-07',具体取决于您的意思)
    7. ORDER BY应该追踪GROUP BY
    8. 据说你的查询可能看起来像

      SELECT sqldate,
             CAST(MAX(CASE WHEN stat_number = 693  THEN sqlvalue END) AS UNSIGNED) AS S693,
             CAST(MAX(CASE WHEN stat_number = 698  THEN sqlvalue END) AS UNSIGNED) AS S698,
             CAST(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED) AS S2010,
             CAST(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED) AS S2020,
             COALESCE(MAX(CASE WHEN stat_number = 693  THEN sqlvalue END) AS UNSIGNED), 0) +
             COALESCE(MAX(CASE WHEN stat_number = 698  THEN sqlvalue END) AS UNSIGNED), 0) +
             COALESCE(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED), 0) +
             COALESCE(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED), 0) AS STOTAL
        FROM statsvalue
       WHERE sqldate > '2013-01-07'
       GROUP BY sqldate
       ORDER BY sqldate
      

      或使用像这样的内联视图

      SELECT sqldate, S693, S698, S2010, S2020,
             COALESCE(S693,  0) + 
             COALESCE(S698,  0) + 
             COALESCE(S2010, 0) + 
             COALESCE(S2020, 0) AS STOTAL
        FROM
      (
        SELECT sqldate
               CAST(MAX(CASE WHEN stat_number = 693  THEN sqlvalue END) AS UNSIGNED) AS S693,
               CAST(MAX(CASE WHEN stat_number = 698  THEN sqlvalue END) AS UNSIGNED) AS S698,
               CAST(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED) AS S2010,
               CAST(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED) AS S2020
          FROM statsvalue
         WHERE sqldate > '2013-01-07'
         GROUP BY sqldate
      ) q
        ORDER BY sqldate