How obtain all of the relationship when the table contains information of the hierarchy?

I have a table containing the information relationship MySQL master & slave, look like this,
Master  Slave
---------- ----------
1  2
1  3
3  4
4  5
4  6
4  7
2  8
11  12
11  13
21  22
The requirement is to gather all of the relationship in a group, the result should look like this.
1,2,3,4,5,6,7,8
11,12,13
21,22
Could someone give me a tip how to do?
Thanks in advance!


Best wishes
Satine

Nikolay Savvinov wrote:

Oops, I misread your original post - you want to break a together in connected groups, it's a little more complicated, sys_connect_by_path probably help here...

It is sys_connect_by_path + connect_by_root + chain aggregation (I used XMLAGG). The only question to OP is MySQL is owned by Oracle, but does not have the Oracle database and my solution won't work in MySQL. In any case:

with sample_table as (
                      select 1 master,2 slave from dual union all
                      select 1,3 from dual union all
                      select 3,4 from dual union all
                      select 4,5 from dual union all
                      select 4,6 from dual union all
                      select 4,7 from dual union all
                      select 2,8 from dual union all
                      select 11,12 from dual union all
                      select 11,13 from dual union all
                      select 21,22 from dual
                     ),
                t as (
                      select  connect_by_root master root,
                              slave
                        from  sample_table
                        start with master not in (select slave from sample_table)
                        connect by master = prior slave
                     )
select  root || ',' || rtrim(xmlagg(xmlelement(s,slave,',').extract('//text()') order by slave),',') path
  from  t
  group by root
  order by root
/

PATH
----------------
1,2,3,4,5,6,7,8
11,12,13
21,22

SQL> 

SY.

Tags: Database

Similar Questions

Maybe you are looking for

  • How can I uninstall the toolbar from firefox

    Inadvertently, I installed the firefox toolbar while seeking manual parts. I would like to remove it, but it does not appear on my program list.

  • USB 3.0 upgrade

    I have a HP pavilin elite model # hpe - 480 t.  What advice due I need to upgrade to usb 3.0

  • No Signal after installing new graphics card

    Hello and thank you in advance for your help! We have a 2 years old HP p6540y.  It has an ATI video card integrated. When we recently turned on, we got a message "no signal" on the monitor and then he'd go to sleep.  We tried many things to fix... li

  • iCloud problem after restore

    Hello I have problem after restoring my iphone 6 1 or 2 weeks ago, I put my apple with this id * but ID not enable find my iphone, this time, my ios is 8.4. before that I didn't put everything on icloud my iphone apple ID 6. before restoring already

  • BSOD on reboot after installing the patch MS10-046

    I installed the critical patch MS10-046 on a netbook under XP SP3. On reboot, bsod with error message: "The Shell32.dll image is possibly corrupt. The header checksum does not match the checksum calculated. » The only way to start was via a USB key.