sql - MySQL - Displaying a record and its parents -


this question has answer here:

how display specific record , parents (maximum two)?

table: +---------------+ | id | parentid | +---------------+ |  1 |     null | |  2 |        1 | |  3 |        1 | |  4 |        2 | |  5 |        2 | |  6 |        2 | +---------------+  expected result: showing record id 4 , 2 parents  +---------------+ | id | parentid | +---------------+ |  1 |     null | |  2 |        1 | |  4 |        2 | +---------------+ 

i don't know how it. following query doesn't work.

(select `c1`.`parentid` `table` `c1` `c1`.`id` = 4 limit 1) `c1parentid`, (select `c2`.`parentid` `table` `c2` `c2`.`id` = `c1parentid` limit 1) `c2parentid`, select * `table` `c3` `c3`.`id` = `c1parentid` or `c3`.`id` = `c2parentid` or `c3`.`id` = 4 

my knowledge of advanced queries poor. please :)

you need cursor , loop. looping child way root (until parentid null).

http://www.mysqltutorial.org/mysql-cursor/


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -