常用SQL笔记MySQL
2025-04-08 23:34阅读:28评论:0
MySQL之删除节点同时删除子节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);