mySQL one-to-many query
Posted
by Stomped
on Stack Overflow
See other posts from Stack Overflow
or by Stomped
Published on 2010-04-11T15:39:59Z
Indexed on
2010/04/11
15:43 UTC
Read the original article
Hit count: 350
mysql
I've got 3 tables that are something like this (simplified here ofc):
- users
- user_id
- user_name
- info
- info_id
- user_id
- rate
- contacts
- contact_id
- user_id
- contact_data
users has a one-to-one relationship with info, although info doesn't always have a related entry.
users has a one-to-many relationship with contacts, although contacts doesn't always have related entries.
I know I can grab the proper 'users' + 'info' with a left join, is there a way to get all the data I want at once?
For example, one returned record might be:
user_id: 5
user_name: tom
info_id: 1
rate: 25.00
contact_id: 7
contact_data: 555-1212
contact_id: 8
contact_data: 555-1315
contact_id: 9
contact_data: 555-5511
Is this possible with a single query? Or must I use multiple?
© Stack Overflow or respective owner