mysql孩子在同一行的父母

时间:2016-04-05 11:31:11

标签: mysql group-concat nested-query

有没有办法让您在SqlFiddle中看到的架构显示为:

SKU | size S | size M | size L | init_qty | qty

? 我试图使用GROUP_CONCAT,这不是我想要的。

这是我的查询

select sku, name, group_concat(taglie separator ';') as `Magazzino`
from
(
  select p.sku, p.name, concat(po.valuename, ':',
  group_concat(po.value separator ',')) as taglie
  from products p
  right join products_opt po
  on p.id = po.product
  group by p.sku
) tbl
group by sku;

有什么建议吗? 谢谢

1 个答案:

答案 0 :(得分:-1)

您需要为AUTO_INCREMENT添加id

SQLFiddle:

http://sqlfiddle.com/#!9/0bde6/2

需要SQL:

select 
    p.sku, 
    sum(po.value = 'S') as `size S`,
    sum(po.value = 'M') as `size M`,
    sum(po.value = 'L') as `size L`,
    po.product as demoproduct
from products p
right join products_opt po
on p.id = po.product
group by p.sku;

输出:

+-------+--------+--------+--------+-------------+
| sku   | size S | size M | size L | demoproduct |
+-------+--------+--------+--------+-------------+
| PROD1 |      1 |      1 |      1 |           1 |
| PROD2 |      2 |      1 |      2 |           2 |
+-------+--------+--------+--------+-------------+