sql - MySQL getting data + recursive query -
i have got 3 tables:
+-----+----------+ +-----+----------+-------+ +-----+----------+-------+ | id | a_id | | a_id| b_id | value | | b_id| b_id_ | value | +-----+----------+ +-----+----------+-------+ +-----+----------+-------+ | 1| 5| | 5| 1| aa| | 1| 2| zzxx| +-----+----------+ +-----+----------+-------+ +-----+----------+-------+ | 2| 3| | 3| 3| bb| | 2| | vvyy| +-----+----------+ +-----+----------+-------+ +-----+----------+-------+ | 3| 4| bbll| +-----+----------+-------+ | 5| | oopp| +-----+----------+-------+ | 4| 5| mmnn| +-----+----------+-------+
what select statement need use, output this(table3 can 4 levels deep self):
+----+------------------------------+ | id | value | +----+------------------------------+ | 1| aa\zzxx\vvyy| +----+------------------------------+ | 2| bb\bbll\mmnn\oopp| +----+------------------------------+
as don't have experience db , sql, hard me. , have no vision how this.
this has done in mysql. hardest thing have read recursive query in mysql since doesn't exist, people have simulate it. have read topics recursive query, understood that's not me.
any appreciated.
by hard , fast learning managed solve problem. code below.
select distinct other.data, concat( '/',ifnull(t4.value,''), case when (t4.value null) '' else '/' end, ifnull(t3.value,''), case when (t3.value null) '' else '/' end, ifnull(t2.value,''), case when (t2.value null) '' else '/' end, ifnull(t1.value,''), case when (t1.value null) '' else '/' end, table2.value ) 'my column name' table1 left join table2 on (table1.a_id = table2.a_id) left join table3 t1 on (t1.b_id = table2.b_id) left join table3 t2 on (t2.b_id = t1.b_id_) left join table3 t3 on (t3.b_id = t2.b_id_) left join table3 t4 on (t4.b_id = t3.b_id_)
big @damodaran , solution recursive query. how create mysql hierarchical recursive query
be careful using code, have used db, queried data. approach might slow on other different usage. if use this, suggest think indexing fields.
Comments
Post a Comment