【问题标题】:MySQL create a stored procedure that loops itselfMySQL 创建一个自循环的存储过程
【发布时间】:2018-04-25 15:23:42
【问题描述】:

我想创建一个 mysql 函数,该函数可以使用表 category 中的列 related 找到所有相关的祖先,然后使用所有这些祖先(子、孙...) id,包括它自己来查找listing_category 中的那些 ID 使用列 category

类别

ID, Related
1,0
2,1
3,1
4,1
5,0
6,5
7,1
8,7
9,7
10,1

如果我选择 1,那么 2,3,4,7,10 是它的孩子,8,9 是它的孙子。

listing_category

Category
1
1
2
3
3
5
6
9
7
7

所以现在我想创建一个 MySql 函数,它可以在另一个名为 listing_category 的表中计算 1、2、3、4、7、10、8、9 的所有实例

   create function listing_count(ID int(11)) returns int(11)
   begin 
    declare count int(11);
    set count=(select count(*) from listing_category where category=ID);
    while (select id from category where related=ID) as childID and count<100 do
     set count=count+listing_count(childID);
    end while;
    return count; 
   end

所以listing_count(1) 会在category 中找到所有亲戚 2,3,4,7,10,8,9,然后计算 1,2,3,4,7,10,8,9 里面的所有实例listing_category。因此在本例中将返回 8 个计数。

可以用mysql存储过程吗?

【问题讨论】:

  • 尝试使用关系将表与自身连接起来并进行计数。如果需要父母 -> 孩子 -> 孙子,则需要加入两次表。
  • 您的9 的返回值似乎不正确。不应该是8吗?
  • @clinomaniac 是的,你是对的。 8就是答案
  • 是否可以在“select in”语句中声明一个数组并使用该数组

标签: mysql stored-procedures


【解决方案1】:

您可以使用递归存储过程来做到这一点。这样做的好处是无论祖先的深度如何,它都可以工作,例如孩子,孙子,曾孙等。

delimiter //
drop PROCEDURE if EXISTS listing_count //
create procedure listing_count(in parentID int(11), out thesum int(11))
begin 
  declare childID int(11);
  declare childSum int(11);
  declare finished int default 0;
  declare childID_cursor cursor for select id from category where related=parentID;
  declare continue handler for not found set finished = 1;
  select count(*) into thesum from listing_category where category=parentID;
  open childID_cursor;
  get_children: LOOP
    fetch childID_cursor into childID;
    if finished = 1 then 
      LEAVE get_children;
    end if;
    call listing_count(childID, childSum);
    set thesum = thesum + childSum;
  end loop get_children;
  close childID_cursor;
end
//

使用您的数据,此查询产生预期结果 (8):

SET @@SESSION.max_sp_recursion_depth=25;
call listing_count(1, @x);
select @x;

如果你真的想要一个函数,你可以将过程封装在一个函数中(因为 MySQL 不允许你创建递归函数):

DELIMITER //
drop function if exists lc//
create function lc(id int(11)) RETURNS int(11)
BEGIN
  declare sum int(11);
  call listing_count(id, sum);
  return sum;
END
//
select lc(1)

输出:

8

【讨论】:

  • 嗨 Maciek,这对你有用吗?它也可能很容易适应您的其他question
【解决方案2】:

如果您希望在一列中包含所有相关类别,则需要将 l1(仅一行)、l2 和 l3 中的所有 ID 合并。

SELECT l1.* FROM category l1
WHERE l1.id = ID
UNION ALL
SELECT l2.* FROM category l1
    INNER JOIN category l2 ON l1.related = l2.id
WHERE l1.id = ID
UNION ALL
SELECT l3.* FROM category l1
    INNER JOIN category l2 ON l1.related = l2.id
    INNER JOIN category l3 ON l2.related = l3.id
WHERE l1.id = ID

一旦你有了所有的 ID,你就可以得到计数:

这里有一个查询会为你计算记录:

SELECT COUNT(*) FROM listing_category 
WHERE category IN (
SELECT l1.* FROM category l1
    WHERE l1.id = ID
    UNION ALL
    SELECT l2.* FROM category l1
        INNER JOIN category l2 ON l1.related = l2.id
    WHERE l1.id = ID
    UNION ALL
    SELECT l3.* FROM category l1
        INNER JOIN category l2 ON l1.related = l2.id
        INNER JOIN category l3 ON l2.related = l3.id
    WHERE l1.id = ID)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2013-08-05
    • 2016-11-11
    • 2017-10-14
    • 2021-07-10
    相关资源
    最近更新 更多