MySQL data to a python dict structure
<p>The structure of my mysql table looks like this:</p>

<pre><code>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
</code></pre>

<p>My desired output is like this :</p>

<pre><code>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]}
        }
</code></pre>

<p>If <code>50:[2016-10-11 10:53:25,2016-10-11 10:53:25]</code> (when you don't have a previews timestamp and you need to duplicate) it's hard to make, just a <code>50:[2016-10-11 10:53:25]</code> can be ok.
How can I extract this to a python dict from my db table.</p>

<p>I tryed something like :</p>

<pre><code>query_all = "SELECT GROUP_CONCAT(mid,liters,timestamp) FROM `my_table` GROUP BY mid  ORDER BY mid "
</code></pre>

<p>But I don't know how to order this. Thanks for your time. </p>

<p><code>GROUP_CONCAT</code> may have own <code>ORDER BY</code></p>

<p>e.g .</p>

<pre><code>GROUP_CONCAT(mid,liters,timestamp ORDER BY liters)
</code></pre>

python mysql sql dictionary