简体   繁体   English

MySQL数据转换为python dict结构

[英]MySQL data to a python dict structure

The structure of my mysql table looks like this: 我的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

My desired output is like this : 我想要的输出是这样的:

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]}
        }

If 50:[2016-10-11 10:53:25,2016-10-11 10:53:25] (when you don't have a previews timestamp and you need to duplicate) it's hard to make, just a 50:[2016-10-11 10:53:25] can be ok. 如果50:[2016-10-11 10:53:25,2016-10-11 10:53:25] (当您没有预览时间戳并且需要复制时),则很难制作,只有50:[2016-10-11 10:53:25]可以。 How can I extract this to a python dict from my db table. 如何从我的数据库表中将其提取到python字典中。

I tryed something like : 我尝试了类似的东西:

query_all = "SELECT GROUP_CONCAT(mid,liters,timestamp) FROM `my_table` GROUP BY mid  ORDER BY mid "

But I don't know how to order this. 但我不知道该如何订购。 Thanks for your time. 谢谢你的时间。

GROUP_CONCAT may have own ORDER BY GROUP_CONCAT可能有自己的ORDER BY

eg . 例如。

GROUP_CONCAT(mid,liters,timestamp ORDER BY liters)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM