Let's say that I have a table called Appointments, another called Clients and a joining table called ClientsAppointments which relates the two aforementioned ones. This is used in a scenario where a client can have several appointments, and several clients can attend to the same appointment (n-n relation).
I would like to list the Appointments with a field "clients" being an array of all the Clients related to that appointment. What I've tried so far:
SELECT * FROM Appointments a
INNER JOIN ClientsAppointments ca ON ca.IdAppointment = a.IdAppointment
INNER JOIN Clients c ON c.IdClient = ca.IdClient
That doesn't work, of course. It gives me a list of appointments repeated with each of the clients they have. Then in PHP I would process them to achieve this. It seemed more efficient this way rather than making multiple queries because there's usually only one client per appointment.
Table schema (these are not the actual tables, but simplified to illustrate the case):
Appointment(
INT idAppointment,
DATETIME start,
DATETIME end)
Clients(
INT idClient,
VARCHAR name)
ClientsAppointments(
INT idAppointment,
INT idClient)