I'm attempting to combine multiple rows into individual rows with multiple columns, where the data is from three tables. I followed the suggestions in MySQL pivot table but the question and answers do not account for multiple joins.
Here are the underlying tables:
table n
+------+----+------+
| name | id | code |
+------+----+------+
| foo | 1 | NULL |
| bar | 2 | z |
| baz | 3 | y |
+------+----+------+
table ac
+------+----+
| code | id |
+------+----+
| h | 1 |
| i | 2 |
+------+----+
table c
+-----+------+-------+
| cid | code | desc |
+-----+------+-------+
| 9 | h | desch |
| 9 | i | desci |
| 8 | z | descz |
| 8 | y | descy |
+-----+------+-------+
Here are the expected results:
+------+-------+-------+
| name | type8 | type9 |
+------+-------+-------+
| foo | null | desch |
| bar | descz | desci |
| baz | descy | null |
+------+-------+-------+
I can get pretty close to the results I want with:
select
n.name,
n.code as type8,
ac.code as type9
from n
left join ac
on ac.id=n.id
but as expected, this yields the codes only:
+------+-------+-------+
| name | type8 | type9 |
+------+-------+-------+
| foo | null | h |
| bar | z | i |
| baz | y | null |
+------+-------+-------+
and I'm interested in replacing the codes with the longer descriptions from table c.