当前位置:网站首页>MySQL function - recursive function
MySQL function - recursive function
2022-04-23 12:21:00 【JunLin_ cavalier】
find_in_set Function USES
FIND_IN_SET(str,strlist)
str String to query
strlist Field name Parameter with ”,” Separate Such as (1,2,6,8)
Exactly match , Separated data
mysql in find_in_set() Use of functions - Ordinary hope - Blog Garden
MySQL function function - Recursive function use
MySql Using functions to realize recursive query - Simple books
Mysql according to id Check all parents or children
Mysql according to id Check all parents or children - Ruiyang intelligence - Blog Garden
mysql recursive query ,mysql From subclass ID Query all parent classes ( It's often used in infinite classification )
because mysql Does not support similar oracle with ...connect Of Recursive query syntax
It has always been thought that similar queries can either be stored procedures or recursive queries can only be written by programs .
Now I found the original one sql Statements can also be done
Let's first look at the structure of the data table as follows :
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
My request is based on a classification ID( This category ID It may be a subcategory ), Get all the parent categories , Here is the corresponding 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
Code @r := 5 Label query id by 5 All the superclasses of . give the result as follows
1, ‘Home’
2, ‘About’
4, ‘Legal’
5, ‘Privacy’
### Declaration header file Participation is 123,12343,145435 In the form of , Recursively find the parent node on all paths If it is single, you can remove while loop , Leave only select part
CREATE DEFINER=`user_name` FUNCTION `getFromTree`(
selectIds VARCHAR (1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, ### Participation is 123,12343,145435 In the form of , Recursively find the parent node on all paths
workId VARCHAR (100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ### Individual other conditions
) RETURNS varchar(1000) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
BEGIN
DECLARE selectId VARCHAR (50); ### Single selectId
DECLARE result VARCHAR (1000); ### Single selectId Parent node found
DECLARE resultListList VARCHAR (1000); ### All parent nodes
DECLARE count INT DEFAULT 0; ### End of calculation
set result = '';
set resultListList = '';
WHILE count = 0 DO ### for The loop processes one at a time ID Find the node
SET selectId = SUBSTRING_INDEX(selectIds, ',', 1);
IF LENGTH(selectId) = LENGTH(selectIds) THEN
SET count = 1; ### Stop conditions , The intercepted length is the same as that of a single
ELSE
SET selectIds = SUBSTRING(......); ### Cut off the front part each time
END IF;
SELECT
GROUP_CONCAT(T2.result_id) into result ### Put the result of the query into a temporary variable ,GROUP_CONCAT yes Additional ,
FROM
(
SELECT
@r AS _id,
(
SELECT
@r := parent_id
FROM
work_tree
WHERE
id = _id
) AS parent_id
FROM
(SELECT @r := selectId) vars, ### Defining variables @r
work_tree
WHERE
@r <> 0 ### The parent node is not 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); ### Add once per cycle
END IF;
END
WHILE;
RETURN resultListList;
END
Single recursion
### Declaration header file Single
CREATE DEFINER=`user_name` FUNCTION `getFromTree`(
selectIds VARCHAR (1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, ### Participation is 123 In the form of , Recursively find the parent node on all paths
workId VARCHAR (100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ### Individual other conditions
) RETURNS varchar(1000) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
BEGIN
DECLARE selectId VARCHAR (50); ### Single selectId
DECLARE result VARCHAR (1000); ### Single selectId Parent node found
SELECT
GROUP_CONCAT(T2.result_id) into result ### Put the result of the query into a temporary variable ,GROUP_CONCAT yes Additional ,
FROM
(
SELECT
@r AS _id,
(
SELECT
@r := parent_id
FROM
work_tree
WHERE
id = _id
) AS parent_id
FROM
(SELECT @r := selectId) vars, ### Defining variables @r
work_tree
WHERE
@r <> 0 ### The parent node is not 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://yzsam.com/2022/04/202204231216221915.html
边栏推荐
- NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
- 没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
- Idea database navigator plug-in
- worder字体网页字体对照表
- On lambda powertools typescript
- c# 设置logo图标和快捷方式的图标
- MySQL函数-递归函数
- How to solve the computer system card?
- Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
- Qt绘制图像
猜你喜欢
Next.js 静态数据生成以及服务端渲染的方式
一个平面设计师的异想世界|ONES 人物
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
How do programmers finalize nucleic acid statistics with 130 lines of code
SPSS之单因素方差分析
QT one process runs another
31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...
C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
[redis series] redis learning 13. Redis often asks simple interview questions
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
随机推荐
力扣-1137.第N个泰波那契数
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
Qt双缓冲绘图
Windows2008系统如何切换PHP版本
第二十四课 经典问题解析
How much does software testing help reduce program bugs?
Qt一个进程运行另一个进程
第二十三课 临时对象
画结果图推荐网址
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
Metalama简介4.使用Fabric操作项目或命名空间
Chapter 5 optimizing queries using in memory expressions (IM 5.1)
什么是网关
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
How to switch PHP version in Windows 2008 system
Metalama简介4.使用Fabric操作项目或命名空间
Castle. Dynamic proxy implements transaction unit control
STM32CubeProgrammer基础使用说明