欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mysql查询某个节点下的所有子节点

程序员文章站 2022-06-08 09:16:34
...

创建表

CREATE TABLE `dynamic_dict` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_id` int(11) NOT NULL,
  `dict_code` varchar(100) NOT NULL,
  `item_code` varchar(100) NOT NULL,
  `parent_item_code` varchar(100) DEFAULT NULL COMMENT '级联父级节点',
  `item_name` varchar(100) NOT NULL,
  `creater` varchar(45) NOT NULL,
  `create_time` datetime NOT NULL,
  `updater` varchar(45) NOT NULL,
  `update_time` datetime NOT NULL,
  `delete_flag` char(1) NOT NULL DEFAULT 'N',
  PRIMARY KEY (`id`),
  KEY `idx_dict_code` (`dict_code`)
) ENGINE=InnoDB AUTO_INCREMENT=216 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

创建数据


INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'yitian', '1', NULL, '父级节点', 'yitain', '2021-05-17 11:34:11', 'yitian', '2021-05-17 11:34:27', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'yitian', '2', '1', '子级节点', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'yitian', '3', '2', '三级节点', 'yitian', '2021-05-17 11:36:06', 'yitian', '2021-05-17 11:36:14', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'yitian', '4', '1', '二级节点', 'yitain', '2021-05-17 14:06:32', 'yitian', '2021-05-17 14:06:39', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'danxuan', '1', '', '单选1', 'yitain', '2021-06-03 19:16:25', 'yitian', '2021-06-03 19:16:29', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'danxuan', '2', '', '单选2', 'yitain', '2021-06-03 19:16:25', 'yitian', '2021-06-03 19:16:29', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian1', 'jilianitem', '1', '二级子节点1', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian1', 'jilianitem1', '1', '二级子节点2', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian1', 'jilianitem2', '2', '二级子节点3', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian1', 'jilianitem3', '2', '二级子节点4', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian2', 'jilianitemcode1', 'jilianitem', '三级子节点1', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian2', 'jilianitemcode2', 'jilianitem1', '三级子节点2', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian2', 'jilianitemcode3', 'jilianitem2', '三级子节点3', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');
INSERT INTO `dynamic_dict`( `company_id`, `dict_code`, `item_code`, `parent_item_code`, `item_name`, `creater`, `create_time`, `updater`, `update_time`, `delete_flag`) VALUES ( 7011, 'jilian2', 'jilianitemcode4', 'jilianitem3', '三级子节点4', 'yitian', '2021-05-17 11:35:10', 'yitian', '2021-05-17 11:35:17', 'N');

解析

-- 查询某个节点【@item_codes := '1'】下的所有子节点  
    -- 解析 
        -- 一、先赋值某个节点,获取所有节点得父节点是这个节点的子节点 FIND_IN_SET( parent_item_code, @item_codes )
        -- 二、赋值所有@item_codeds是最新的子节点的当前item_code @item_codeds := @item_codes【循环】
        -- 三、获取所有的节点 FIND_IN_SET( b.item_code, ID._item_codes )  结果排序

SELECT      
	ID.level, 
	b.item_code, 
	b.item_name AS itemName, 
	b.parent_item_code AS parentItemCode  
FROM 
	( 
	SELECT 
		@item_codeds := @item_codes AS _item_codes					
        --  三、查询所有parent_item_code在@item_codes中的数据
		(SELECT @item_codes := GROUP_CONCAT( item_code ) FROM	dynamic_dict  WHERE FIND_IN_SET( parent_item_code, @item_codes )) AS citem_codes, 
		@l := @l + 1 AS level  -- 二、设置当前等级
	FROM 
		dynamic_dict, 
		(SELECT @item_codes := '1', @l := 0) bb    --  一、开始 1、给item_codes 赋值节点数据  2、给@l赋值节点等级0
		WHERE @item_codes IS NOT NULL 
	) id, dynamic_dict b 
	WHERE 
	FIND_IN_SET( b.item_code, ID._item_codes ) 
	ORDER BY LEVEL,id 

相关标签: sql