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?
Each row of result must have the same columns, so you can’t aggregate multiple rows of contact not having the other columns as well.
Hopefully, this query would achieve what you need: