通过父级id查询子类下所有的id

表结构: 

/*
 Navicat Premium Data Transfer

 Source Server         : 本地
 Source Server Type    : MySQL
 Source Server Version : 50738
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50738
 File Encoding         : 65001

 Date: 26/01/2024 16:17:53
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for dy_category_manage
-- ----------------------------
DROP TABLE IF EXISTS `dy_category_manage`;
CREATE TABLE `dy_category_manage` (
                                      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
                                      `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1班会课  2教学师训 3 德育师训 4 问题手册 ',
                                      `category_name` varchar(20) NOT NULL COMMENT '分类名',
                                      `category_parent_id` bigint(20) unsigned DEFAULT '0' COMMENT '分类父级id',
                                      `full_path` varchar(255) NOT NULL COMMENT '全路径',
                                      `level` tinyint(2) NOT NULL COMMENT '层级',
                                      `sort` int(10) unsigned NOT NULL COMMENT '排序',
                                      `deleted` tinyint(2) NOT NULL DEFAULT 0 COMMENT '0:未删除 1:删除 ',
                                      `create_id` bigint(11) DEFAULT NULL COMMENT '创建者id',
                                      `update_id` bigint(11) DEFAULT NULL COMMENT '修改者id',
                                      PRIMARY KEY (`id`) USING BTREE,
                                      KEY `idx_type` (`type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='师训分类管理';

-- ----------------------------
-- Records of dy_category_manage
-- ----------------------------
BEGIN;
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (1, 3, '生涯教育', 0, '/1/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (2, 3, '生涯教学能力提升', 1, '/1/2/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (3, 3, '新高考落地培训', 1, '/1/3/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (4, 3, '心理健康教育', 0, '/4/', 1, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (5, 3, '班级管理能力提升', 4, '/4/5/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (6, 3, '学生心理咨询技巧', 4, '/4/6/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (7, 3, '心理中心工作指导', 4, '/4/7/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (8, 3, '师德师风建设', 0, '/8/', 1, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (9, 3, '名家讲堂:林崇德教授讲师德', 8, '/8/9/', 2, 1, 1, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (10, 2, '名家讲堂', 0, '/10/', 1, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (11, 2, '核心素养解读', 10, '/10/11/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (12, 2, '新教材解读', 10, '/10/12/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (13, 2, '新教材培训', 0, '/13/', 1, 5, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (14, 2, '专家培训讲座', 11, '/11/14/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (15, 2, '一线教师实践分享', 13, '/13/15/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (16, 2, '专家示范课', 13, '/13/16/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (17, 4, '个体问题解决思路', 0, '/17/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (18, 4, '厌学', 17, '/17/18/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (19, 4, '不遵守课堂秩序', 17, '/17/19/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (20, 4, '青春期恋爱', 17, '/17/20/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (21, 4, '考试挫折', 17, '/17/21/', 2, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (22, 4, '过度使用网络', 17, '/17/22/', 2, 5, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (23, 4, '人际冲突', 17, '/17/23/', 2, 6, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (24, 4, '校园欺凌', 17, '/17/24/', 2, 7, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (25, 4, '校园抽烟行为', 17, '/17/25/', 2, 8, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (26, 4, '偷盗行为', 17, '/17/26/', 2, 9, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (27, 4, '选科困难', 17, '/17/27/', 2, 10, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (28, 4, '升学通道选择困难', 17, '/17/28/', 2, 11, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (29, 4, '高校升学材料准备困难', 17, '/17/29/', 2, 12, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (30, 4, '群体问题解决思路', 0, '/30/', 1, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (31, 4, '班级风气不佳', 30, '/30/31/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (32, 4, '适应困难', 30, '/30/32/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (33, 4, '人际关系问题', 30, '/30/33/', 2, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (34, 4, '学习状态较差', 30, '/30/34/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (35, 4, '规则意识薄弱', 31, '/30/31/35/', 3, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (36, 4, '学习态度不端正', 31, '/30/31/36/', 3, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (37, 4, '缺乏凝聚力', 31, '/30/31/37/', 3, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (38, 4, '新高一入学不适应', 32, '/30/32/38/', 3, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (39, 4, '开学无法收心', 32, '/30/32/39/', 3, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (40, 4, '班级欺凌', 33, '/30/33/40/', 3, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (41, 4, '异性交往过密', 33, '/30/33/41/', 3, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (42, 4, '学习动力较弱', 34, '/30/34/42/', 3, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (43, 4, '自控力较弱', 34, '/30/34/43/', 3, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (44, 4, '考试心态不佳', 34, '/30/34/44/', 3, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (45, 4, '缺乏学习方法', 34, '/30/34/45/', 3, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (46, 4, '突发事件应对思路', 0, '/46/', 1, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (47, 4, '当事人心理干预指导', 46, '/46/47/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (48, 4, '有关人员心理干预指导', 46, '/46/48/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (49, 4, '家人心理干预指导', 46, '/46/49/', 2, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (50, 4, '首页', 0, '/50/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (51, 4, '学生问题指导手册说明', 50, '/50/51/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (52, 4, '整体说明', 46, '/46/52/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (53, 1, '生涯规划', 0, '/53/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (54, 1, '生涯唤醒', 53, '/53/54/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (55, 1, '自我探索', 53, '/53/55/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (56, 1, '探索世界', 53, '/53/56/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (57, 1, '生涯行动', 53, '/53/57/', 2, 4, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (58, 1, '学习心理', 0, '/58/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (59, 1, '高考心理提升', 58, '/58/59/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (60, 1, '学习潜能激发', 58, '/58/60/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (61, 1, '学习习惯养成', 58, '/58/61/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (62, 1, '思想品德', 0, '/62/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (63, 1, '心理健康', 0, '/63/', 1, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (64, 1, '新生适应', 63, '/63/64/', 2, 1, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (65, 1, '人际交往', 63, '/63/65/', 2, 2, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (66, 1, '人格塑造', 63, '/63/66/', 2, 3, 0, -1, -1);
INSERT INTO `dy_category_manage` (`id`, `type`, `category_name`, `category_parent_id`, `full_path`, `level`, `sort`, `deleted`, `create_id`, `update_id`) VALUES (67, 1, '情绪调节', 63, '/63/67/', 2, 4, 0, -1, -1);
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

存储过程: -- 生产环境禁止使用

CREATE DEFINER=`dev_user`@`%` FUNCTION `getChildIds`(`parentId` VARCHAR(32)) RETURNS varchar(4000) CHARSET utf8mb4
BEGIN
       DECLARE sTemp VARCHAR(4000);
       DECLARE sTempChd VARCHAR(4000);
       SET sTemp = '';
       SET sTempChd =cast(parentId as CHAR);
       WHILE sTempChd is not null DO
         SET sTemp = concat(sTemp,',',sTempChd);
         SELECT group_concat(id) INTO sTempChd FROM dy_category_manage where FIND_IN_SET(category_parent_id,sTempChd)>0;
       END WHILE;
       RETURN sTemp;
END

sql查询 :

 SELECT
	    t3.id
    FROM
        (
            SELECT
                t1.id,
            IF( find_in_set( category_parent_id, @pids ) > 0, @pids := concat( @pids, ',', t1.id ), 0 ) AS ischild
            FROM
                ( SELECT id, category_parent_id FROM dy_category_manage t WHERE t.deleted = 0 ORDER BY category_parent_id, id ) t1,
                ( SELECT @pids := #{categoryId} id ) t2
        ) t3
    WHERE ischild !=0

https://www.cnblogs.com/rainydayfmb/p/8028868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值