在多列中加入多行

时间:2013-05-09 17:50:26

标签: php mysql

我的查询结果中有多行:

例如,表" extras":

id_extras - id_order - id_size - name_extra - price

1           1          3         Teddy        4
2           2          8         Balloon      8
3           2          9         Wine         2
4           2          1         Choco        9
5           6          1         Bag          4

我真正想要的是:

id_extras - id_order - id_size - name_extra - price - name_extra1 - name_extra2 - price1 - price2

1           1          3         Teddy        4
2           2          8         Balloon      8       Wine          Choco         2        9
3           6          1         Bag          4

所以我需要name_extraprice在列而不是行中,我现在就知道了。

SELECT  * ,
        MAX(CASE WHEN id_order = id_order THEN name_extra ELSE NULL END) name_extra1,
                MAX(CASE WHEN id_order = id_order THEN name_extra ELSE NULL END) name_extra2,
                MAX(CASE WHEN id_order = id_order THEN price ELSE NULL END) price1,
                MAX(CASE WHEN id_order = id_order THEN price ELSE NULL END) price2
FROM    extras 


GROUP   BY id_order

0 个答案:

没有答案