微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想要在mysql的这段代码中垂直和水平方向上所有行的总和吗?

如何解决我想要在mysql的这段代码中垂直和水平方向上所有行的总和吗?

SET @a=5;
SET @en=9;
WITH recursive cte (k) 
AS(
        SELECT @a 
        UNION ALL
        SELECT k + 1
        FROM cte WHERE k < @en
  )
        SELECT GROUP_CONCAT(CONCAT('concat(m*',k,')as "','"')) INTO @tab FROM cte;

SET @sql = CONCAT('WITH recursive cte1 (m) 
AS (
        SELECT 1 
        UNION ALL
        SELECT m + 1  
        FROM cte1 WHERE m < 10
    ) 
        SELECT ',@tab,' FROM cte1;'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

解决方法

要水平求和,您必须将所有值相加。

SELECT 5,6,7,8,9,5+6+7+8+9;

要垂直汇总它们,可以使用WITH ROLLUP

一个例子:

select a,sum(b) 
from (select 1 as a,11 as b 
      union all 
      select 2,22 
      union all select 3,33)x 
GROUP by a WITH ROLLUP;

输出:

+------+--------+
| a    | sum(b) |
+------+--------+
|    1 |     11 |
|    2 |     22 |
|    3 |     33 |
| NULL |     66 |
+------+--------+

编辑:以上应用于您的代码(对您的代码进行了一些更改!)给出:

SET @a=5;
SET @en=9;
WITH recursive cte (k,n) 
AS(
        SELECT @a,@a
        UNION ALL
        SELECT k + 1,n+k+1
        FROM cte WHERE k < @en
  )
        SELECT CONCAT(GROUP_CONCAT(CONCAT('sum(m*',k,')as "','"')),',sum(m*',MAX(n),') as sum') INTO @tab FROM cte;

SET @SQL = CONCAT('WITH recursive cte1 (m) 
AS (
        SELECT 1 
        UNION ALL
        SELECT m + 1  
        FROM cte1 WHERE m < 10
    ) 
        SELECT ',@tab,' FROM cte1 GROUP BY m WITH ROLLUP;'
);
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

输出:

+-----+-----+-----+-----+-----+------+
| 5   | 6   | 7   | 8   | 9   | sum  |
+-----+-----+-----+-----+-----+------+
|   5 |   6 |   7 |   8 |   9 |   35 |
|  10 |  12 |  14 |  16 |  18 |   70 |
|  15 |  18 |  21 |  24 |  27 |  105 |
|  20 |  24 |  28 |  32 |  36 |  140 |
|  25 |  30 |  35 |  40 |  45 |  175 |
|  30 |  36 |  42 |  48 |  54 |  210 |
|  35 |  42 |  49 |  56 |  63 |  245 |
|  40 |  48 |  56 |  64 |  72 |  280 |
|  45 |  54 |  63 |  72 |  81 |  315 |
|  50 |  60 |  70 |  80 |  90 |  350 |
| 275 | 330 | 385 | 440 | 495 | 1925 |
+-----+-----+-----+-----+-----+------+

运行代码后,一些其他信息:

SELECT @tab;的输出:

+---------------------------------------------------------------------------------------------+
| @tab                                                                                        |
+---------------------------------------------------------------------------------------------+
| sum(m*5)as "5",sum(m*6)as "6",sum(m*7)as "7",sum(m*8)as "8",sum(m*9)as "9",sum(m*35) as sum |
+---------------------------------------------------------------------------------------------+

SELECT @SQL \G的输出:

*************************** 1. row ***************************
@sql: WITH recursive cte1 (m)
AS (
        SELECT 1
        UNION ALL
        SELECT m + 1
        FROM cte1 WHERE m < 10
    )
        SELECT sum(m*5)as "5",sum(m*35) as sum FROM cte1 GROUP BY m WITH ROLLUP;

EDIT2:为什么在@a中两次输入:

如果您像这样从cte中选择值:

SET @a=5;
SET @en=9;
WITH recursive cte (k,n+k+1
        FROM cte WHERE k < @en
  )
select k,n from cte;

输出为:

+------+------+
| k    | n    |
+------+------+
|    5 |    5 |
|    6 |   11 |
|    7 |   18 |
|    8 |   26 |
|    9 |   35 |
+------+------+

在答案中使用max(n),因为它是所有k值的总和。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。