StrangeWill Posted September 6, 2008 Share Posted September 6, 2008 So I've been working on trying to get the information I want out of one query, but the way the database is structured seems to make it extremely difficult to do such. I don't even know if I can restructure it in a way to get the information I want out of one query. So in my mind I say "why not just run more than one query?", alas how many queries is too many to run during a page load? They're all pretty simple queries (two pieces of data, one left join, where an ID is a value). Problem is I could need to do this some 20-30 times, or even more. Is this okay, or should I be looking at another method? Just in case another method is suggested, I have this (for example) as the table: id | parentid | text 1 | 0 | Sometext 2 | 1 | ChildText 3 | 1 | OtherChildText 4 | 2 | ChildChildText Currently I can left join the table again on tablejoined.parentid = table.id, alas this only gets me one deep, and also returns a lot of data (will return the id and text of the parent again and again and again for every child item, seems like a waste of memory/time). I would need something that is recursive till there are no children. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 6, 2008 Share Posted September 6, 2008 Can you show us a code or give us a scenario of what you're trying to do? Quote Link to comment Share on other sites More sharing options...
StrangeWill Posted September 6, 2008 Author Share Posted September 6, 2008 Heh, just gave it. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted September 6, 2008 Share Posted September 6, 2008 do you have a sample query that would guide us what you want to get? Quote Link to comment Share on other sites More sharing options...
StrangeWill Posted September 6, 2008 Author Share Posted September 6, 2008 Currently I'm roughly running with this (sorry if it isn't exact, but it's a general idea, I don't have the SQL statement right in front of me at the moment..) SELECT table.*, tablechild.* FROM table LEFT JOIN table AS tablechild ON tablechild.parentid = table.id However that only gets me one child deep. I could just do a loop: for(each record) { SELECT table.* FROM table WHERE parentid=currentTableID } Quote Link to comment Share on other sites More sharing options...
fenway Posted September 6, 2008 Share Posted September 6, 2008 Well, you can keep on left join-ing, but this model of data organization is not meant to be recursive -- there's the list adjacency model, though. Quote Link to comment Share on other sites More sharing options...
StrangeWill Posted September 6, 2008 Author Share Posted September 6, 2008 Well, you can keep on left join-ing, but this model of data organization is not meant to be recursive -- there's the list adjacency model, though. Yeah, and it also tends to send way more data than is actually needed, and ends up requiring a hefty overhead of dealing with the data. Should I just loop through queries, or create a separate table, then do a call if the parent HAS children? Quote Link to comment Share on other sites More sharing options...
fenway Posted September 8, 2008 Share Posted September 8, 2008 Well, how many levels deep are we talking about, realistically? Quote Link to comment Share on other sites More sharing options...
StrangeWill Posted September 8, 2008 Author Share Posted September 8, 2008 Well, how many levels deep are we talking about, realistically? Max of 2-3, no more. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 10, 2008 Share Posted September 10, 2008 Then you can self-join in that many tables. Quote Link to comment Share on other sites More sharing options...
StrangeWill Posted September 10, 2008 Author Share Posted September 10, 2008 Okay, I'll just do that. Thanks. 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.