I have a typical N-M relationship structure like this:
two independent tables:
Table Client:
id | name
1 | Joe
2 | Marc
and
Table Product:
id | name
1 | Toyota
2 | Ford
3 | Fiat
and a linking table like:
Table Purchase:
idClient | idProduct | Paid
1 | 1 | 3000
1 | 2 | 2999
2 | 3 | 4500
2 | 1 | 1000
I would like a query that produces:
client | Toyota | Ford | Fiat
Joe | 3000 | 2999 |
Marc | 1000 | | 4500
is there some way to do it in MySQL?
Thanks !
Like @StuartAinsworth said, you need to do a Pivot Query.
For a dynamic approach use this: