SQL Query将一列的数据拆分为多列

时间:2012-06-01 15:53:25

标签: php mysql sql

我正在尝试使用HTML表格构建一个PHP页面,该表格将填充此查询的结果。请注意,我想要拆分成多列的值最初都在同一列中....“lastvalue”这使得编写“ONE”查询很难输出我想要的结果。

表格布局:

name                                    | key_                  |  lastvalue
Active Calls per T1 SL5/DI1             | activeChannels[33]    |   1
Active Calls per T1 SL5/DI2             | activeChannels[34]    |   1
Active Calls per T1 SL5/DI3             | activeChannels[35]    |   2
Active Calls per T1 SL5/DI4             | activeChannels[36]    |   1
Active Calls per T1 SL5/DI5             | activeChannels[37]    |   2
Active Calls per T1 SL5/DI6             | activeChannels[38]    |   1
Active Calls per T1 SL5/DI7             | activeChannels[39]    |   0
Active Calls per T1 SL5/DI8             | activeChannels[40]    |   0
Active Calls per T1 SL6/DI1             | activeChannels[41]    |   0
Active Calls per T1 SL6/DI2             | activeChannels[42]    |   0
Active Calls per T1 SL6/DI3             | activeChannels[43]    |   0
Active Calls per T1 SL6/DI4             | activeChannels[44]    |   0
Active Calls per T1 SL6/DI5             | activeChannels[45]    |   0
Active Calls per T1 SL6/DI6             | activeChannels[46]    |   0
Active Calls per T1 SL6/DI7             | activeChannels[47]    |   0
Active Calls per T1 SL6/DI8             | activeChannels[48]    |   0
Active Calls per T1 SL7/DI1             | activeChannels[49]    |   23
Active Calls per T1 SL7/DI2             | activeChannels[50]    |   1
Active Calls per T1 SL7/DI3             | activeChannels[51]    |   0
Active Calls per T1 SL7/DI4             | activeChannels[52]    |   9
Active Calls per T1 SL7/DI5             | activeChannels[53]    |   0
Active Calls per T1 SL7/DI6             | activeChannels[54]    |   2
Active Calls per T1 SL7/DI7             | activeChannels[55]    |   0
Active Calls per T1 SL7/DI8             | activeChannels[56]    |   0
Active Calls per T1 SL8/DI1             | activeChannels[57]    |   0
Active Calls per T1 SL8/DI2             | activeChannels[58]    |   0
Active Calls per T1 SL8/DI3             | activeChannels[59]    |   0
Active Calls per T1 SL8/DI4             | activeChannels[60]    |   0
Active Calls per T1 SL8/DI5             | activeChannels[61]    |   0
Active Calls per T1 SL8/DI6             | activeChannels[62]    |   0
Active Calls per T1 SL8/DI7             | activeChannels[63]    |   0
Active Calls per T1 SL8/DI8             | activeChannels[64]    |   0
Active Calls per T1 Totals:             | activeChannels[999    |   42
DS1 Datalink layer Status (L2) SL5/DI1  | statusLayer2[33]      |  Active
DS1 Datalink layer Status (L2) SL5/DI2  | statusLayer2[34]      |  Active
DS1 Datalink layer Status (L2) SL5/DI3  | statusLayer2[35]      |  Active
DS1 Datalink layer Status (L2) SL5/DI4  | statusLayer2[36]      |  Active
DS1 Datalink layer Status (L2) SL5/DI5  | statusLayer2[37]      |  Active
DS1 Datalink layer Status (L2) SL5/DI6  | statusLayer2[38]      |  Active
DS1 Datalink layer Status (L2) SL5/DI7  | statusLayer2[39]      |  Active
DS1 Datalink layer Status (L2) SL5/DI8  | statusLayer2[40]      |  Active
DS1 Datalink layer Status (L2) SL6/DI1  | statusLayer2[41]      |  Active
DS1 Datalink layer Status (L2) SL6/DI2  | statusLayer2[42]      |  Awaiting Establishment
DS1 Datalink layer Status (L2) SL6/DI3  | statusLayer2[43]      |  Active
DS1 Datalink layer Status (L2) SL6/DI4  | statusLayer2[44]      |  Active
DS1 Datalink layer Status (L2) SL6/DI5  | statusLayer2[45]      |  Active
DS1 Datalink layer Status (L2) SL6/DI6  | statusLayer2[46]      |  Active
DS1 Datalink layer Status (L2) SL6/DI7  | statusLayer2[47]      |  Active
DS1 Datalink layer Status (L2) SL6/DI8  | statusLayer2[48]      |  Active
DS1 Datalink layer Status (L2) SL7/DI1  | statusLayer2[49]      |  Active
DS1 Datalink layer Status (L2) SL7/DI2  | statusLayer2[50]      |  Active
DS1 Datalink layer Status (L2) SL7/DI3  | statusLayer2[51]      |  Active
DS1 Datalink layer Status (L2) SL7/DI4  | statusLayer2[52]      |  Active
DS1 Datalink layer Status (L2) SL7/DI5  | statusLayer2[53]      |  Awaiting Establishment
DS1 Datalink layer Status (L2) SL7/DI6  | statusLayer2[54]      |  Active
DS1 Datalink layer Status (L2) SL7/DI7  | statusLayer2[55]      |  Active
DS1 Datalink layer Status (L2) SL7/DI8  | statusLayer2[56]      |  Active
DS1 Datalink layer Status (L2) SL8/DI1  | statusLayer2[57]      |  Active
DS1 Datalink layer Status (L2) SL8/DI2  | statusLayer2[58]      |  Active
DS1 Datalink layer Status (L2) SL8/DI3  | statusLayer2[59]      |  Active
DS1 Datalink layer Status (L2) SL8/DI4  | statusLayer2[60]      |  Active
DS1 Datalink layer Status (L2) SL8/DI5  | statusLayer2[61]      |  Active
DS1 Datalink layer Status (L2) SL8/DI6  | statusLayer2[62]      |  Active
DS1 Datalink layer Status (L2) SL8/DI7  | statusLayer2[63]      |  Active
DS1 Datalink layer Status (L2) SL8/DI8  | statusLayer2[64]      |  Active
DS1 Datalink layer Status (L2) Totals:  | statusLayer2[999]     |  30 Active
DS1 Physical layer Status (L1) SL5/DI1  | statusLayer1[33]      |  Active
DS1 Physical layer Status (L1) SL5/DI2  | statusLayer1[34]      |  Active
DS1 Physical layer Status (L1) SL5/DI3  | statusLayer1[35]      |  Active
DS1 Physical layer Status (L1) SL5/DI4  | statusLayer1[36]      |  Active
DS1 Physical layer Status (L1) SL5/DI5  | statusLayer1[37]      |  Active
DS1 Physical layer Status (L1) SL5/DI6  | statusLayer1[38]      |  Active
DS1 Physical layer Status (L1) SL5/DI7  | statusLayer1[39]      |  Active
DS1 Physical layer Status (L1) SL5/DI8  | statusLayer1[40]      |  Active
DS1 Physical layer Status (L1) SL6/DI1  | statusLayer1[41]      |  Active
DS1 Physical layer Status (L1) SL6/DI2  | statusLayer1[42]      |  Lost Framing
DS1 Physical layer Status (L1) SL6/DI3  | statusLayer1[43]      |  Active
DS1 Physical layer Status (L1) SL6/DI4  | statusLayer1[44]      |  Active
DS1 Physical layer Status (L1) SL6/DI5  | statusLayer1[45]      |  Active
DS1 Physical layer Status (L1) SL6/DI6  | statusLayer1[46]      |  Active
DS1 Physical layer Status (L1) SL6/DI7  | statusLayer1[47]      |  Active
DS1 Physical layer Status (L1) SL6/DI8  | statusLayer1[48]      |  Active
DS1 Physical layer Status (L1) SL7/DI1  | statusLayer1[49]      |  Active
DS1 Physical layer Status (L1) SL7/DI2  | statusLayer1[50]      |  Active
DS1 Physical layer Status (L1) SL7/DI3  | statusLayer1[51]      |  Active
DS1 Physical layer Status (L1) SL7/DI4  | statusLayer1[52]      |  Active
DS1 Physical layer Status (L1) SL7/DI5  | statusLayer1[53]      |  Lost Framing
DS1 Physical layer Status (L1) SL7/DI6  | statusLayer1[54]      |  Active
DS1 Physical layer Status (L1) SL7/DI7  | statusLayer1[55]      |  Active
DS1 Physical layer Status (L1) SL7/DI8  | statusLayer1[56]      |  Active
DS1 Physical layer Status (L1) SL8/DI1  | statusLayer1[57]      |  Active
DS1 Physical layer Status (L1) SL8/DI2  | statusLayer1[58]      |  Active
DS1 Physical layer Status (L1) SL8/DI3  | statusLayer1[59]      |  Active
DS1 Physical layer Status (L1) SL8/DI4  | statusLayer1[60]      |  Active
DS1 Physical layer Status (L1) SL8/DI5  | statusLayer1[61]      |  Active
DS1 Physical layer Status (L1) SL8/DI6  | statusLayer1[62]      |  Active
DS1 Physical layer Status (L1) SL8/DI7  | statusLayer1[63]      |  Active
DS1 Physical layer Status (L1) SL8/DI8  | statusLayer1[64]      |  Active
DS1 Physical layer Status (L1) Totals:  | statusLayer1[999]     |  30 Active
Framing Errors per T1 SL5/DI1           | frameErrors[33]       |   22
Framing Errors per T1 SL5/DI2           | frameErrors[34]       |   62
Framing Errors per T1 SL5/DI3           | frameErrors[35]       |   64
Framing Errors per T1 SL5/DI4           | frameErrors[36]       |   66
Framing Errors per T1 SL5/DI5           | frameErrors[37]       |   57
Framing Errors per T1 SL5/DI6           | frameErrors[38]       |   59
Framing Errors per T1 SL5/DI7           | frameErrors[39]       |   66
Framing Errors per T1 SL5/DI8           | frameErrors[40]       |   64
Framing Errors per T1 SL6/DI1           | frameErrors[41]       |   68
Framing Errors per T1 SL6/DI2           | frameErrors[42]       |   68
Framing Errors per T1 SL6/DI3           | frameErrors[43]       |   65
Framing Errors per T1 SL6/DI4           | frameErrors[44]       |   75
Framing Errors per T1 SL6/DI5           | frameErrors[45]       |   64
Framing Errors per T1 SL6/DI6           | frameErrors[46]       |   69
Framing Errors per T1 SL6/DI7           | frameErrors[47]       |   68
Framing Errors per T1 SL6/DI8           | frameErrors[48]       |   58
Framing Errors per T1 SL7/DI1           | frameErrors[49]       |   194
Framing Errors per T1 SL7/DI2           | frameErrors[50]       |   196
Framing Errors per T1 SL7/DI3           | frameErrors[51]       |   185
Framing Errors per T1 SL7/DI4           | frameErrors[52]       |   180
Framing Errors per T1 SL7/DI5           | frameErrors[53]       |   0
Framing Errors per T1 SL7/DI6           | frameErrors[54]       |   199
Framing Errors per T1 SL7/DI7           | frameErrors[55]       |   59
Framing Errors per T1 SL7/DI8           | frameErrors[56]       |   66
Framing Errors per T1 SL8/DI1           | frameErrors[57]       |   28
Framing Errors per T1 SL8/DI2           | frameErrors[58]       |   47
Framing Errors per T1 SL8/DI3           | frameErrors[59]       |   43
Framing Errors per T1 SL8/DI4           | frameErrors[60]       |   41
Framing Errors per T1 SL8/DI5           | frameErrors[61]       |   48
Framing Errors per T1 SL8/DI6           | frameErrors[62]       |   42
Framing Errors per T1 SL8/DI7           | frameErrors[63]       |   47
Framing Errors per T1 SL8/DI8           | frameErrors[64]       |   45
Framing Errors per T1 Totals:           | frameErrors[999]      |  2415

以下是我想要的结果:

port        | layer1        |   layer2                  | freaming_errors   | active_calls
T1 SL5/DI1  | Active        |   Active                  | 22                | 1
T1 SL5/DI2  | Active        |   Active                  | 62                | 1
T1 SL5/DI3  | Active        |   Active                  | 64                | 2
T1 SL5/DI4  | Active        |   Active                  | 66                | 1
T1 SL5/DI5  | Active        |   Active                  | 57                | 2
T1 SL5/DI6  | Active        |   Active                  | 59                | 1
T1 SL5/DI7  | Active        |   Active                  | 66                | 0
T1 SL5/DI8  | Active        |   Active                  | 64                | 0
T1 SL6/DI1  | Active        |   Active                  | 68                | 0
T1 SL6/DI2  | Lost Framing  |   Awaiting Establishment  | 68                | 0
T1 SL6/DI3  | Active        |   Active                  | 65                | 0
T1 SL6/DI4  | Active        |   Active                  | 75                | 0
T1 SL6/DI5  | Active        |   Active                  | 64                | 0
T1 SL6/DI6  | Active        |   Active                  | 69                | 0
T1 SL6/DI7  | Active        |   Active                  | 68                | 0
T1 SL6/DI8  | Active        |   Active                  | 58                | 0
T1 SL7/DI1  | Active        |   Active                  | 194               | 23
T1 SL7/DI2  | Active        |   Active                  | 196               | 1
T1 SL7/DI3  | Active        |   Active                  | 185               | 0
T1 SL7/DI4  | Active        |   Active                  | 180               | 9
T1 SL7/DI5  | Lost Framing  |   Awaiting Establishment  | 0                 | 0
T1 SL7/DI6  | Active        |   Active                  | 199               | 2
T1 SL7/DI7  | Active        |   Active                  | 59                | 0
T1 SL7/DI8  | Active        |   Active                  | 66                | 0
T1 SL8/DI1  | Active        |   Active                  | 28                | 0
T1 SL8/DI2  | Active        |   Active                  | 47                | 0
T1 SL8/DI3  | Active        |   Active                  | 43                | 0
T1 SL8/DI4  | Active        |   Active                  | 41                | 0
T1 SL8/DI5  | Active        |   Active                  | 48                | 0
T1 SL8/DI6  | Active        |   Active                  | 42                | 0
T1 SL8/DI7  | Active        |   Active                  | 47                | 0
T1 SL8/DI8  | Active        |   Active                  | 45                | 0
T1 Totals:  | 30 Active     |   30 Active               | 2415              | 42

有什么想法吗?.....谢谢

1 个答案:

答案 0 :(得分:0)

您必须使用MYSQL中的SUBSTRLOCATE函数的复杂组合。

有关SUBSTR click here

的详细信息

有关LOCATE click here

的详细信息