• <i id='YZsxv'><tr id='YZsxv'><dt id='YZsxv'><q id='YZsxv'><span id='YZsxv'><b id='YZsxv'><form id='YZsxv'><ins id='YZsxv'></ins><ul id='YZsxv'></ul><sub id='YZsxv'></sub></form><legend id='YZsxv'></legend><bdo id='YZsxv'><pre id='YZsxv'><center id='YZsxv'></center></pre></bdo></b><th id='YZsxv'></th></span></q></dt></tr></i><div id='YZsxv'><tfoot id='YZsxv'></tfoot><dl id='YZsxv'><fieldset id='YZsxv'></fieldset></dl></div>

    <small id='YZsxv'></small><noframes id='YZsxv'>

      1. <legend id='YZsxv'><style id='YZsxv'><dir id='YZsxv'><q id='YZsxv'></q></dir></style></legend>
        <tfoot id='YZsxv'></tfoot>
          <bdo id='YZsxv'></bdo><ul id='YZsxv'></ul>

        递归调用自身的mysql存储过程

        时间:2023-06-03
        <legend id='fY0y5'><style id='fY0y5'><dir id='fY0y5'><q id='fY0y5'></q></dir></style></legend>
        <tfoot id='fY0y5'></tfoot>
          <tbody id='fY0y5'></tbody>

            • <bdo id='fY0y5'></bdo><ul id='fY0y5'></ul>
              <i id='fY0y5'><tr id='fY0y5'><dt id='fY0y5'><q id='fY0y5'><span id='fY0y5'><b id='fY0y5'><form id='fY0y5'><ins id='fY0y5'></ins><ul id='fY0y5'></ul><sub id='fY0y5'></sub></form><legend id='fY0y5'></legend><bdo id='fY0y5'><pre id='fY0y5'><center id='fY0y5'></center></pre></bdo></b><th id='fY0y5'></th></span></q></dt></tr></i><div id='fY0y5'><tfoot id='fY0y5'></tfoot><dl id='fY0y5'><fieldset id='fY0y5'></fieldset></dl></div>

              <small id='fY0y5'></small><noframes id='fY0y5'>

                1. 本文介绍了递归调用自身的mysql存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有下表:

                  id | parent_id | quantity
                  -------------------------
                  1  | null      | 5
                  2  | null      | 3
                  3  | 2         | 10
                  4  | 2         | 15
                  5  | 3         | 2
                  6  | 5         | 4
                  7  | 1         | 9
                  

                  现在我需要 mysql 中的一个存储过程,它递归地调用自己并返回计算出的数量.例如,id 6 的父值为 5,父值为 3,父值为 2.所以我需要计算 4 * 2 * 10 * 3 ( = 240) 结果.

                  Now I need a stored procedure in mysql that calls itself recursively and returns the computed quantity. For example the id 6 has 5 as a parent which as 3 as a parent which has 2 as a parent. So I need to compute 4 * 2 * 10 * 3 ( = 240) as a result.

                  我对存储过程还很陌生,以后不会经常使用它们,因为我更喜欢将业务逻辑放在程序代码中而不是数据库中.但在这种情况下,我无法避免.

                  I am fairly new to stored procedures and I won't use them very often in the future because I prefer having my business logic in my program code rather then in the database. But in this case I can't avoid it.

                  也许一个 mysql 大师(就是你)可以在几秒钟内编写一个工作语句.

                  Maybe a mysql guru (that's you) can hack together a working statement in a couple of seconds.

                  推荐答案

                  仅在mysql版本>=5

                  its work only in mysql version >= 5

                  存储过程声明是这样的,

                  the stored procedure declaration is this,

                  你可以给它一点改进,但这个工作:

                  you can give it little improve , but this working :

                  DELIMITER $$
                  
                  CREATE PROCEDURE calctotal(
                     IN number INT,
                     OUT total INT
                  )
                  
                  BEGIN
                  
                     DECLARE parent_ID INT DEFAULT NULL ;
                     DECLARE tmptotal INT DEFAULT 0;
                     DECLARE tmptotal2 INT DEFAULT 0;
                  
                     SELECT parentid   FROM test   WHERE id = number INTO parent_ID;   
                     SELECT quantity   FROM test   WHERE id = number INTO tmptotal;     
                  
                     IF parent_ID IS NULL
                      THEN
                      SET total = tmptotal;
                     ELSE     
                      CALL calctotal(parent_ID, tmptotal2);
                      SET total = tmptotal2 * tmptotal;   
                     END IF;
                  
                  END$$
                  
                  DELIMITER ;
                  

                  呼叫就像(设置这个变量很重要):

                  the calling is like (its important to set this variable) :

                  SET @@GLOBAL.max_sp_recursion_depth = 255;
                  SET @@session.max_sp_recursion_depth = 255; 
                  
                  CALL calctotal(6, @total);
                  SELECT @total;
                  

                  这篇关于递归调用自身的mysql存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL UPDATE 和 SELECT 一次完成 下一篇:MySQL 表名中的特殊字符

                  相关文章

                    <bdo id='WHpTd'></bdo><ul id='WHpTd'></ul>
                  <tfoot id='WHpTd'></tfoot>

                    <legend id='WHpTd'><style id='WHpTd'><dir id='WHpTd'><q id='WHpTd'></q></dir></style></legend>

                    <i id='WHpTd'><tr id='WHpTd'><dt id='WHpTd'><q id='WHpTd'><span id='WHpTd'><b id='WHpTd'><form id='WHpTd'><ins id='WHpTd'></ins><ul id='WHpTd'></ul><sub id='WHpTd'></sub></form><legend id='WHpTd'></legend><bdo id='WHpTd'><pre id='WHpTd'><center id='WHpTd'></center></pre></bdo></b><th id='WHpTd'></th></span></q></dt></tr></i><div id='WHpTd'><tfoot id='WHpTd'></tfoot><dl id='WHpTd'><fieldset id='WHpTd'></fieldset></dl></div>

                      <small id='WHpTd'></small><noframes id='WHpTd'>