Mysql链选择查询 [英] Mysql Chain Select Query

查看:126
本文介绍了Mysql链选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其表格看起来有点像这样:

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

例如:A链表示a-> B-> C-> D-> E,而C链表示C-> D-> E

For example : Chain of A means a->B->C->D->E whereas Chain of C means C->D->E

我正在使用mysql数据库。

I am using mysql database.

提前感谢

推荐答案

我只是尝试在Mysql中的循环结构并获得成功。只发布到共享 -

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

这篇关于Mysql链选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆