查询PIVOT ou SUM

时间:2017-12-13 15:10:50

标签: mysql sql

我在mysql中选择了这个:

  select product_id, presentation, price from variant limit 3;
      +------------+-------------------------+-------+
      | product_id | presentation            | price |
       +------------+-------------------------+-------+
       |       1608 | CCE 1401 / 2092         |     9 |
       |       1604 | 5 Peças = R$ 5,00 cada  |    25 |
         +------------+-------------------------+-------+

然后我做了一个SUM:

select product_id,   sum(case when presentation =   'Unitário = R$   11,90 ' THEN price END) presentation1,   sum(case when  presentation = '5  Peças = R$ 5,00 cada' THEN price END) presentation2,    sum(case when presentation = 'Bluesky Todas' THEN price END)      presentation3 from variant where product_id = 1604

结果是:

           +------------+---------------+---------------+---------------+
        | product_id | presentation1 | presentation2 | presentation3 |
        +------------+---------------+---------------+---------------+
        |       1604 |          11.9 |            25 |            15 |
        +------------+---------------+---------------+---------------+
        1 row in set (0,00 sec)

我的问题是,我如何让它变得动态?没有把产品的介绍名称?

0 个答案:

没有答案
相关问题