MySQL如何取消动态列

时间:2018-12-31 18:53:43

标签: mysql

我有下表:

表名称:STAT

Region   M07  M08 M09 
---------------------
P1       0    1   0    
P2       0    0   0   
P3       2    0   0    
P4       0    0   0     
P5       0    0   0     
P6       0    0   0   
P7       9    0   3  

我想取消月份列,其中包含数值。此月的列标题是动态的,并且会发生变化,但是总列数将保持不变。

预期结果

---------------------
Region   Month  Qty
---------------------
P1      M07    0
P1      M08    1
P1      M09    0
P2      M07    0
P2      M08    0
P2      M09    0

如果我能帮助将此SQL语句编写到UNPIVOT,然后考虑数字列的动态性质,那将是很棒的。 它应该用mySQL编写。

1 个答案:

答案 0 :(得分:1)

不幸的是,mysql没有public class Bar { // Optional c'tor if param(s) are required to be initialized for typical use // Accessor for e public MyEnum e { get { return m_BarInterop.e; } set { m_BarInterop.e = value; } } // Accessor for s public uint s { get { VerifyUIntPtrFitsInUint(m_BarInterop.s); // will throw an exception if value out of range return (uint)m_BarInterop.s; } set { // uint will always fit in UIntPtr m_BarInterop.s = (UIntPtr)value; } } // Interop-compatible 'Bar' structure (not required to be inner struct) [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] internal struct Bar_Interop { public MyEnum e; public System.UIntPtr s; } // Instance of interop-compatible 'Bar' structure internal Bar_Interop m_BarInterop; } 函数。

您最好的选择是UNPIVOT解决方案,例如:

UNION ALL

可以创建一段SQL代码(例如,作为一个存储过程),该代码将通过首先查询SELECT region, 'M07' AS month, M07 AS qty from stat UNION ALL SELECT region, 'M08', M08 AS from stat UNION ALL SELECT region, 'M09', M09 AS from stat 检索列名并使用结果来动态生成此SQL查询。查询文字。