我的mysql表的结构如下所示:
id | mid | liters | timestamp
1 | 20 | 50 | 2016-10-11 10:53:25
2 | 30 | 60 | 2016-10-11 10:40:20
3 | 20 | 100 | 2016-10-11 10:09:27
4 | 30 | 110 | 2016-10-11 09:55:07
5 | 40 | 80 | 2016-10-11 09:44:46
6 | 40 | 90 | 2016-10-11 07:56:14
7 | 20 | 120 | 2016-04-08 13:27:41
8 | 20 | 130 | 2016-04-08 15:35:28
我想要的输出是这样的:
dict = {
20:{50:[2016-10-11 10:53:25,2016-10-11 10:53:25],100:[2016-10-11 10:53:25,2016-10-11 10:09:27],120:[2016-10-11 10:09:27,2016-04-08 13:27:41],130:[2016-04-08 13:27:41,2016-04-08 15:35:28]},
30:{60:[2016-10-11 10:40:20,2016-10-11 10:40:20],110:[2016-10-11 10:40:20,2016-10-11 09:55:07]}
40:{80:[2016-10-11 09:44:46,2016-10-11 09:44:46],90:[2016-10-11 09:44:46,2016-10-11 07:56:14]}
}
如果50:[2016-10-11 10:53:25,2016-10-11 10:53:25]
(当您没有预览时间戳并且需要复制时)很难做到,只需50:[2016-10-11 10:53:25]
即可。
如何从db表中将其提取到python dict。
我尝试过类似的东西:
query_all = "SELECT GROUP_CONCAT(mid,liters,timestamp) FROM `my_table` GROUP BY mid ORDER BY mid "
但我不知道如何订购。谢谢你的时间。
答案 0 :(得分:0)
GROUP_CONCAT
可能拥有自己的ORDER BY
例如
GROUP_CONCAT(mid,liters,timestamp ORDER BY liters)