adrianle Posted September 1, 2016 Share Posted September 1, 2016 Hey y'all.. need some definite help here.I have two tables, one for Parents, and one for Children. There may be multiple Children for any given Parent, and they're both related in their tables via a unique FamilyID.I need to pull each Parent record, and ALL related Children records into a recordset.Ex. Parent Bill has two kids, Jimmy & Tommy. Parent Dave has one kid, Molly.I need to retrieve a recordset that looks like this: Bill, Jimmy, Tommy Dave, Molly Obviously, one of the challenges is that the different number of Children that will be output - though I'm fine with retrieving multiple children info into a single column.Suggestions? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 1, 2016 Share Posted September 1, 2016 try SELECT parent.name as parent , GROUP_CONCAT(child.name) as children FROM parent JOIN child USING (familyID) GROUP BY parent.familyID; Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 1, 2016 Share Posted September 1, 2016 (edited) You should have one table, not two. What if the children have children, then they are also parents. This is no different than products and "Sub Categories." id - name - parent_id See the The Adjacency List Model here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ Edited September 1, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 1, 2016 Share Posted September 1, 2016 You should have one table, not two. What if the children have children, then they are also parents. I agree with you in theory. However, without knowing the purpose of this application it is impossible to know if the relationship needs to go deeper than just the parent-child relationship. It could be an application where the user (parent) enters their data and their children's records. So, a person's child would create their own account for adding their children's records. However, I'm guessing this is for a class assignment. If so, I would hope the instructor is actually having them use two tables with the express intent of then adding more 'complexity' in order to illustrate the need for a single table. With programming I think it is better to let students make these types of mistakes and then show them the proper way to do it. Otherwise, they never really appreciate why they should be doing it that way. Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 1, 2016 Share Posted September 1, 2016 it is impossible to know if the relationship needs to go deeper than just the parent-child relationship. Still no need for a second table even in that case. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 2, 2016 Share Posted September 2, 2016 Still no need for a second table even in that case. Again, not enough information to make that assertion. The data needed for the parent could be vastly different than the children. 1 Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 2, 2016 Share Posted September 2, 2016 How about it OP? What exactly do you have going on? 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.