I have a database table which includes physical education lessons. A lesson can have multiple lesson types like "warming-up", "game" and "technical instruction". Those types are stored in another table. I have a third table which matches the types to lessons.
| lessonId | title |
|----------|-------------|
| 1 | Test Lesson |
| typeId | label |
|--------|-----------------------|
| 1 | warming-up |
| 2 | game |
| 3 | technical instruction |
| lessonId | typeId |
|----------|--------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
I use PDO in PHP to retrieve the lessons from the database. Now I'd like to get the lesson and it's types with one query. The result (encoded as JSON) should look like this. It would be fine if the query gives the types back as a comma-separated list, which I can then parse with PHP.
{
"lessonId" : 1,
"title" : "Test Lesson",
"types" : [
{
"typeId" : 1,
"label" : "warming-up"
},
{
"typeId" : 2,
"label" : "game"
},
{
"typeId" : 3,
"label" : "technical instruction"
}
]
}
What SQL query should I use to get this result?