Jump to content

Need help on getting table results


mabus

Recommended Posts

My situation involves querying two tables. One table contains records that match the other in a one to many relationship.

Example

  Person

    - Id

    - Name

 

  FavoriteColor

    -Id

    - Name

    - PersonId.

 

The example above shows what my situation is like. This basically means that each peroson can have 0 or more FavoriteColors.

Now, in this case that I want to come up with a table that contains the list of person, regardless if they have or not have any favorite colors.

 

Sample Output (desired ouput)

 

PersonName            ColorName

John                          Blue

John                          Red

Doe                       

James                        Green

Bill                     

 

How will I be able to come up with a result like that? What I want is to be able to display results, even if a particular person profile has a matching color or not.

 

Thanks in advance

Link to comment
Share on other sites

You need to join the tables where the Id from the 'Person' table equals the PersonId from the 'FavoriteColor' table.  Select Name from the Person and Favorite Color table WHERE p.Id = fc.PersonId (to match the correct person and FavColor)  Then loop through and display results.  Try it and let me know if you need help.

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.