Jump to content

Parent & Children


adrianle

Recommended Posts

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?

Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.