I have a database with a table which looks somewhat like this :
Root | Connector
A | B
B | C
C | D
D | E
E | -
So i want to retrieve the chain for any given root till the connector is blank
For example : Chain of A means a->B->C->D->E whereas Chain of C means C->D->E
I am using mysql database.
Thanks in advance
解决方案
I just try with loop structure in Mysql and got success. Posting to share only--
CREATE PROCEDURE `root_connect`(IN init char(1),OUT str char(15))
BEGIN
set @startChar:=(select connector from tableName where root = init);
set @endloop := "no";
set @fullchar:= @startChar;
set @newchar:= "";
if (@startChar !="-" OR @startChar =null) then
WHILE (@endloop = "no") DO
set @newchar :=(select connector from tableName where root = @startChar);
if(@newchar = '-') THEN
set @endloop := "yes";
else
set @fullchar:= concat(@fullchar,"-",@newchar);
end if;
set @startChar := @newchar;
END WHILE;
end if;
select @fullchar;
END