MySQL - 连接表并将行转换为列

时间:2015-03-03 17:01:38

标签: mysql join pivot

我有两个类似于这些的表(t_stamp通常是DATETIME,为清楚起见在此缩写):

datapoints    
+------+---------+----+---------+
| ndx  |  value  | ID | t_stamp |
+------+---------+----+---------+
|  1   | 503.42  |  1 | 3/1/15  |
|  2   | 17.81   |  2 | 3/1/15  |
|  4   | 498.21  |  1 | 3/2/15  |
|  4   | 19.51   |  2 | 3/2/15  |
+------+---------+----+---------+

parameters    
+------+----+---------------+-------+
| ndx  | ID | description   | unit  |
+------+----+---------------+-------+
|  1   |  1 | wetwell level |  ft   |
|  2   |  2 | effluent flow |  MGD  |
+------+----+---------------+-------+

我希望将它们组合起来,以便描述成为列标题并按时间戳的顺序列出值,最终结果如下所示:

new table    
+---------+---------------+---------------+
| t_stamp | wetwell level | effluent flow |
+---------+---------------+---------------+
| 3/1/15  |    503.42     |    17.81      |
| 3/2/15  |    498.21     |    19.51      |
+---------+---------------+---------------+

请记住,每张表中的行数要多得多,所以我正在寻找动态的东西。它可以是基于查询或存储过程。谢谢你的帮助!

0 个答案:

没有答案