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