OK, I need to get some data from multiple tables, all of which have a key.
Projects (key:id)
Projects_users (key:project_id,user_id)
Users (key:id)
Basically each project can have N users on it. I need to get all the users that are in each project in a formatted response.
For instance:
projects = {[
1: {
'title':'Project 1',
'users': [
1: { id: 23, name:'john' },
2: { id: 55, name:'sally' }
]
}
2: {
'title':'Project 2',
'users': [
1: { id: 41, name:'jeff' },
2: { id: 55, name:'sally' }
]
}
]}
Also, I understand I can do this in PHP with multiple MYSQL statements. Would that be faster and do the concatenation in PHP, or letting MYSQL do this be more efficient / quick.
You can use following statement to get the desired data:
After that, you can traverse the resulting table in single pass and build the array you want.