Show tree result between two table related with comma

54 Views Asked by At

In MySQL my table1 is

ID name parent
1  one   0
2  two   1
3  three 1

and my table2 is

ID name parent
1  com    2,3 -->is table1.ID

I want to relate between table2.parent and table1.id and show tree result :

com -> one -> two,three

How can I query it? I query this:

SELECT  *
from table1 a
left join table1 b on b.parent=a.ID
where b.ID in (2,3)

this work nice but didn't work this:

SELECT  *
from table1 a
left join table1 b on b.parent=a.ID
where b.ID in (select parent from table2)
1

There are 1 best solutions below

0
On

try this:

select *
from table2 t2
left join table1 t1 on find_in_set(t1.id, t2.parent) > 0
group by t2.id
;