jimmyoneshot Posted December 8, 2011 Share Posted December 8, 2011 In my database I have a parent child relationship set up within my services table. On my front end this can only go three levels deep. Currently I am using the following query to get all items at their levels:- SELECT id, @a:='1' AS level FROM services WHERE parentId = 0 UNION SELECT id, @a:='2' AS level FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId = 0) UNION SELECT id, @a:='3' AS level FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId = 0)) UNION SELECT id, @a:='4' AS level FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId IN (SELECT id FROM services WHERE parentId = 0))) I'm wondering is there a better method of doing this as this seems like overkill? Quote Link to comment Share on other sites More sharing options...
jimmyoneshot Posted December 8, 2011 Author Share Posted December 8, 2011 I also need to show parent child relationship so I can produce an indented visual of these i.e. it will look something like this:- -itemA -itemB -itemC -itemD -itemE -itemF Quote Link to comment Share on other sites More sharing options...
fenway Posted December 8, 2011 Share Posted December 8, 2011 One of the stickies has a dozen ways to handle this. Quote Link to comment Share on other sites More sharing options...
jimmyoneshot Posted December 11, 2011 Author Share Posted December 11, 2011 Thanks fenway. I ended up looping through the result with php but I'll look into that to see if I can simplify it with mysql. Cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.