MySQL dynamic-pivot(MySQL 动态枢轴)
问题描述
我有一张像这样的产品零件表:
I have a table of product parts like this:
零件
而且我想要一个返回这样的表的查询:
and I want a query that will return a table like this:
在其实际实施中将有数百万个产品部件
In its actual implementation there will be millions of product parts
推荐答案
不幸的是,MySQL 没有 PIVOT
函数,但您可以使用聚合函数和 CASE
对其进行建模代码> 语句.对于动态版本,您将需要使用准备好的语句:
Unfortunately, MySQL does not have a PIVOT
function but you can model it using an aggregate function and a CASE
statement. For a dynamic version, you will need to use prepared statements:
参见SQL Fiddle with Demo
如果您只有几列,那么您可以使用静态版本:
If you had only a few columns, then you can use a Static version:
这篇关于MySQL 动态枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!