由於2-天,我想找到一個解決方案...
SQL連接映射表,並取回列不列
我有兩個表:
- 組件 - colums: ID |名稱|說明
- components_ingredients - colums: component_id | ingredient_id
=>一個組件可以有多種成分
所以當我加入我的發言表:
SELECT * FROM組分c INNER JOIN components_ingredients CI ON c.id = ci.component_id
我爲表ci中的每種成分找回一行。但是我想回到只有一行與匹配的成分作爲額外的列,如:
c.id | c.name | c.description | ci.ingredient1 | ci.ingredient2 | ci.ingredient3 ...
這是可能的,何時?
感謝
你在找什麼是PIVOT。類似的問題有多種答案。這裏是一個:http://stackoverflow.com/questions/7674786/mysql-pivot-table – Kevin