In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to make an infinite-level parent-son relationship query in a single sentence in MySQL. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
In SQL Server, it is easy to do infinite-level parent-child relationship queries with CTE expressions; in versions that do not support CTE expressions, function recursion is also easy.
In MySQL, the instance of this requirement is a little more complex. In MySQL, there is no recursive query, no table-valued function, and the function does not support recursion, so it is usually implemented in a loop, which is quite awkward. Today I saw a recursive query implemented with a single statement. I have a unique idea. Share it.
Related mysql video tutorials
Table structure and data
CREATE TABLE table1 (id int, name varchar (10), parent_id int); INSERT table1 VALUES (1, 'Home', 0), (2,' About', 1), (3, 'Contact', 1), (4,' Legal', 2), (5, 'Privacy', 4), (6,' Products', 1), (7, 'Support', 2)
Query all parents of id = 5
SELECT ID.level, DATA.* FROM (SELECT @ id as _ id, (SELECT @ id: = parent_id FROM table1 WHERE id = @ id) as _ pid, @ l: = @ lumb1 as level FROM table1, (SELECT @ id: = 5, @ l: = 0) b WHERE @ id > 0) ID, table1 DATA WHERE ID._id = DATA.id ORDER BY level
According to this parent query method, it is easy to write to check all children, and the following query id=2 all children
SELECT ID.level, DATA.* FROM (SELECT @ ids as _ ids, (SELECT @ ids: = GROUP_CONCAT (id) FROM table1 WHERE FIND_IN_SET (parent_id, @ ids)) as cids, @ l: = @ lumb1 as level FROM table1, (SELECT @ ids: = '1century, @ l: = 0) b WHERE @ ids IS NOT NULL) id Table1 DATA WHERE FIND_IN_SET (DATA.id, ID._ids) ORDER BY level, id's method of infinitely hierarchical parent-child relationship query for single sentences in MySQL is shared here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.