当前位置:网站首页>MySQL函数-递归函数
MySQL函数-递归函数
2022-04-23 12:16:00 【JunLin_cavalier】
find_in_set函数使用
FIND_IN_SET(str,strlist)
str 要查询的字符串
strlist 字段名 参数以”,”分隔 如 (1,2,6,8)
精确匹配 ,分隔的数据
mysql中find_in_set()函数的使用 - 平凡希 - 博客园
MySQL function函数 - 递归函数使用
Mysql 根据id查所有父级或子级
Mysql 根据id查所有父级或子级 - 锐洋智能 - 博客园
mysql递归查询,mysql中从子类ID查询所有父类(做无限分类经常用到)
由于mysql 不支持类似 oracle with ...connect的 递归查询语法
之前一直以为类似的查询要么用存储过程要么只能用程序写递归查询.
现在发现原来一条sql语句也是可以搞定的
先来看数据表的结构如下:
id name parent_id
---------------------------
1 Home 0
2 About 1
3 Contact 1
4 Legal 2
5 Privacy 4
6 Products 1
7 Support 1
我要的要求是根据一个分类ID(这个分类ID可能是一个子分类),得到所有的父分类,下面是相应的SQL:
SELECT T2.id, T2.name
FROM (
SELECT
@r AS _id,
(SELECT @r := parent_id FROM table1 WHERE id = _id) AS parent_id,
@l := @l + 1 AS lvl
FROM
(SELECT @r := 5, @l := 0) vars,
table1 h
WHERE @r <> 0) T1
JOIN table1 T2
ON T1._id = T2.id
ORDER BY T1.lvl DESC
代码@r := 5标示查询id为5的所有父类。结果如下
1, ‘Home’
2, ‘About’
4, ‘Legal’
5, ‘Privacy’
### 声明头文件 入参是 123,12343,145435 的形式,递归找出所有路径上的父节点 如果是单个的可以去掉while循环,只留下select部分
CREATE DEFINER=`user_name` FUNCTION `getFromTree`(
selectIds VARCHAR (1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, ### 入参是 123,12343,145435 的形式,递归找出所有路径上的父节点
workId VARCHAR (100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ### 单个其他条件
) RETURNS varchar(1000) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
BEGIN
DECLARE selectId VARCHAR (50); ### 单个selectId
DECLARE result VARCHAR (1000); ### 单个selectId找到的父节点
DECLARE resultListList VARCHAR (1000); ### 所有父节点
DECLARE count INT DEFAULT 0; ### 计算结束
set result = '';
set resultListList = '';
WHILE count = 0 DO ### for循环每次处理一个 ID去找节点
SET selectId = SUBSTRING_INDEX(selectIds, ',', 1);
IF LENGTH(selectId) = LENGTH(selectIds) THEN
SET count = 1; ### 停止条件,截取到长度和单个一样
ELSE
SET selectIds = SUBSTRING(......); ### 每次截掉完成前面的一部分
END IF;
SELECT
GROUP_CONCAT(T2.result_id) into result ### 将查询的结果放入临时变量,GROUP_CONCAT是 追加,
FROM
(
SELECT
@r AS _id,
(
SELECT
@r := parent_id
FROM
work_tree
WHERE
id = _id
) AS parent_id
FROM
(SELECT @r := selectId) vars, ### 定义变量@r
work_tree
WHERE
@r <> 0 ### 父节点不为0
) T1
JOIN work_tree T2 ON T1._id = T2.id
WHERE
T2.workId = workId and (T2.type !=2 or T2.parent_id = 0);
IF result is not null THEN
SET resultListList = concat(resultListList,',',result); ### 每次循环添加一次
END IF;
END
WHILE;
RETURN resultListList;
END
单个递归
### 声明头文件 单个
CREATE DEFINER=`user_name` FUNCTION `getFromTree`(
selectIds VARCHAR (1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, ### 入参是 123 的形式,递归找出所有路径上的父节点
workId VARCHAR (100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ### 单个其他条件
) RETURNS varchar(1000) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
BEGIN
DECLARE selectId VARCHAR (50); ### 单个selectId
DECLARE result VARCHAR (1000); ### 单个selectId找到的父节点
SELECT
GROUP_CONCAT(T2.result_id) into result ### 将查询的结果放入临时变量,GROUP_CONCAT是 追加,
FROM
(
SELECT
@r AS _id,
(
SELECT
@r := parent_id
FROM
work_tree
WHERE
id = _id
) AS parent_id
FROM
(SELECT @r := selectId) vars, ### 定义变量@r
work_tree
WHERE
@r <> 0 ### 父节点不为0
) T1
JOIN work_tree T2 ON T1._id = T2.id
WHERE
T2.workId = workId and (T2.type !=2 or T2.parent_id = 0);
RETURN result;
END
版权声明
本文为[JunLin_cavalier]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40301202/article/details/124322220
边栏推荐
- 远程访问家里的树莓派(上)
- Chapter 4 specifies the attribute of the inmemory column on the no inmemory table for im enabled filling objects: examples (Part IV of im-4.4)
- Purpose of IM expression (IM 5.2)
- Idea code quality specification plug-in sonarlint
- 获取钉钉考勤机打卡记录
- Win10 splash screen after startup
- 《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
- Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
- 激活函数之relu函数
- Outsourcing for five years, abandoned
猜你喜欢
Here comes the detailed picture and text installation tutorial of H5 game
SPSS之单因素方差分析
Win10 splash screen after startup
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
Running error: unable to find or load the main class com xxx. Application
编程辅助工具推荐:图片工具snipaste
IDEA 代码质量规范插件SonarLint
VMware虚拟机使用esxi 导出硬盘vmdk文件
智能多线弹性云增加独立的IP地址,如何实现多线功能?
随机推荐
智能多线弹性云增加独立的IP地址,如何实现多线功能?
激活函数之relu函数
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
如果你是一个Golang面试官,你会问哪些问题?
How do programmers finalize nucleic acid statistics with 130 lines of code
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
用户接口和IM表达式(IM 5.6)
Basic software testing Day2 - Case Execution
Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
Win10 splash screen after startup
[redis series] redis learning 13. Redis often asks simple interview questions
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
ThinkPHP adds image text watermark to generate promotion poster with QR code
Idea code formatting plug-in save actions
远程访问家里的树莓派(上)
QT draw image
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
一文详解头部位姿估计【收藏好文】
Running error: unable to find or load the main class com xxx. Application