MySQL tree structure recursive query

Source: Internet
Author: User

Oracle has been used before, and for tree queries you can use the start with ... connect by

Select *  from  with id='130000'by= Prior parent_id;

Yes, that's what Oracle supports.

Now the company is using MySQL, for this query method can only be achieved through the SQL statement

Languages are interlinked, not to mention SQL.

MySQL does not have its own syntax support, but you can create a function to implement a recursive query.

As shown in ...

Directly on the SQL statement

Create Table' nodelist ' (' ID ')int( One), ' Nodecontent 'varchar( -), ' PID 'int( One)); Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('1','a',NULL);Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('2','b','1');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('3','C','1');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('4','D','2');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('5','e','3');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('6','F','3');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('7','g','5');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('8','h','7');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('9','I','8');Insert  into' NodeList ' (' id ', ' nodecontent ', ' pid ')Values('Ten','J','8');

Then create a function

DROP FUNCTION IF EXISTS' Getchild ' $$CREATEDefiner=' Root ' @ ' localhost 'FUNCTION' Getchild ' (rootidINT)RETURNS VARCHAR( +) CHARSET UTF8BEGIN        DECLAREPtempVARCHAR( +); DECLARECtempVARCHAR( +); SETPtemp= '#'; SETCtemp=CAST(Rootid as CHAR);  whileCtemp is  not NULL DoSETPtemp=CONCAT (Ptemp,',', ctemp); SELECTGroup_concat (ID) intoCtemp fromnodelistWHEREFind_in_set (pid,ctemp)>0; END  while; RETURNptemp; END$ $DELIMITER;

OK, the query can be done by using a function as a query condition.

SELECT *  from WHERE Find_in_set (ID, Getchild (3))

Reference HTTPS://WWW.JIANSHU.COM/P/F99665266BB1

The built-in function used inside the HTTPS://BAIJIAHAO.BAIDU.COM/S?ID=1595349117525189591&WFR=SPIDER&FOR=PC

As long as you can think of, there is a corresponding solution, fortunately, you should step on some pits others realize to you fill out.

MySQL tree structure recursive query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.