MySQL recursive query

Source: Internet
Author: User

SET stemp= ' $ ';
SET stempchd = CAST (Id as CHAR); --Conversion data format

While Stempchd was not NULL do--start loop
SET stemp= CONCAT (stemp, ', ', stempchd); --string concatenation
SELECT Group_concat (ID) into Stempchd from ' user ' WHERE find_in_set (pid,stempchd) >0;

--Group_concat () calculates which rows belong to the same group and divides the same set of parameters into strings separated by commas;

--Find_in_set (PID,STEMPCHD) to determine whether the PID is in Stempchd

--the statement means querying the user table, the PID of the data being queried is in Stempchd, and the ID found is a comma-separated string, re-assigned to Stempchd; when Stempchd has a value, loops again, no value, jumps out of the loop,

--the data in the loop is spelled into a stemp string
END while;

--handling of Stemp
SELECT LENGTH (stemp)-length (replace (stemp, ', ', ')) into Invitorn;
SET Invitorn = Invitorn + 1;
While J <= Invitorn do
SELECT Substring_index (stemp, ', ', J) into TempStr;
SELECT SUBSTRING (tempstr,m+1) into Tempuserid;
SET M = LENGTH (tempstr) + 1;
SET j = J +1;
IF Tempuserid! = UserId Then
Xxxx
END IF;
END while;

MySQL recursive query

Related Article

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.