MySQL pivot row成动态列数可以说我有三个不同的MySQL表:表products:id | name 1 Product A 2 Product B表partners:id | name 1 Partner A 2 Partner B表sales:partners_id | products_id 1 2 2 5 1 5 1 3 1 4 1 5 2 2 2 4 2 3 1 1我想得到一个表格,其中包含行和产品中的合作伙伴作为列。到目前为止,我能够得到这样的输出:name | name | COUNT( * )Partner A Product A 1Partner A Product B 1Partner A Product C 1Partner A Product D 1Partner A Product E 2Partner B Product B 1Partner B Product C 1Partner B Product D 1Partner B Product E 1使用此查询:SELECT partners.name, products.name, COUNT( * ) FROM salesJOIN products ON sales.products_id = products.idJOIN partners ON sales.partners_id = partners.idGROUP BY sales.partners_id, sales.products_idLIMIT 0 , 30但我希望有类似的东西:partner_name | Product A | Product B | Product C | Product D | Product EPartner A 1 1 1 1 2Partner B 0 1 1 1 1问题是,我不知道我将拥有多少产品,因此列号需要根据products表中的行动态更改。
添加回答
举报
0/150
提交
取消