MySQL Recursive hierarchy query

Source: Internet
Author: User

First, you need to understand the native functions of the two MySQL Find_in_set and group_concat:

Examples of Use:


SELECT find_in_set (' A ', ' a,b,c,d ');

Operation Result: 1;


SELECT Group_concat (CDBH) from Ss_menu;

Operation Result: 1,3,4,6,7,8,9,10,...... such as


To create a tree-shaped recursive function:

Begin        declare stemp varchar (;   )       declare stempchd varchar (;     )     SET sTemp =  ' $ ';          Set stempchd =cast (Rootid as char);         while  sTempChd is not null DO              set stemp = concat (Stemp, ', ', stempchd);              select group_concat (CDBH)  into stempchd from  ss_menu             where find_in_ SET (SJCD,STEMPCHD) >0;         END WHILE;           return stemp; end/* query This function */select * from ss_menu m where  Find_in_set (Cdbh, func_menus (0))  ORDER BY sjcd;


The result set returned is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/91/wKioL1cjAyTjGkKOAABp-jQtioo675.png "title=" Menu.png "alt=" Wkiol1cjaytjgkkoaabp-jqtioo675.png "/>



Reference Document: Http://www.2cto.com/database/201209/152513.html

This article is from the "Pig Flying" blog, please be sure to keep this source http://jiyanle.blog.51cto.com/6932197/1768987

MySQL Recursive hierarchy 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.