MySQL之删除节点同时删除子节点

DELIMITER //
drop procedure if exists  sys_columns_nodes//
create procedure sys_columns_nodes(in rootid int)  
begin  
    declare stemp varchar(1000);/*定义一个临时字段来存放所有的类别与子类别*/
    declare stempchd varchar(1000);/*定义一个临时字段,来得到当前类别的子类别*/
    set stemp = '$';  
    set stempchd =rootid;  
  	/*set stempchd =cast(rootid as char); */

    while stempchd>0 do
        set stemp = concat(stemp,',',stempchd);/*将以前类别与现在查询类别进行合并*/
        /*将每次查到的子id形成一个字符组,放到stempchd里,如果stempchd为null就停止循环*/
        select group_concat(id) into stempchd from sys_columns where find_in_set(parent_id,stempchd)>0;  
    end while;  
    delete from sys_columns where find_in_set(id,stemp);

end;
    //
DELIMITER ;

/* 1 是id值*/
call sys_columns_nodes(1);
posted @ 2025-04-08 23:34  程序员の奇妙冒险  阅读(12)  评论(0)    收藏  举报