mysql group by組內排序的實現方式

在做group by和order by 數據篩選排序

1.數據記錄

mysql> select id,sn,uid,uid_from,money,created_at from orders where uid_from = 7 and 
status = 1;
+-----+------------------+-----+----------+-------+---------------------+
| id  | sn               | uid | uid_from | money | created_at          |
+-----+------------------+-----+----------+-------+---------------------+
|  83 | 2019070197101102 |   7 |        7 | 0.01  | 2019-07-01 10:03:06 |
|  98 | 2019070554504899 |   7 |        7 | 0.01  | 2019-07-05 14:28:38 |
|  99 | 2019070597571025 |  17 |        7 | 0.01  | 2019-07-05 10:29:14 |
| 100 | 2019070554985351 |  19 |        7 | 0.01  | 2019-07-05 10:29:26 |
| 101 | 2019070550984910 |  17 |        7 | 0.01  | 2019-07-05 13:45:38 |
+-----+------------------+-----+----------+-------+---------------------+
5 rows in set

mysql> 

2.uid代表用戶ID,查詢需求,用戶進行分組,要查出用戶的money總額和購買次數,以及最後的操作時間。

符合最後操作時間的用戶ID爲7的最後操作時間id是98,用戶ID爲17的jd是101

對uid進行分組,用created_at進行排序,查詢結果

 

mysql> select id,sn,uid,uid_from,created_at,sum(money) as buy_count_money,count(*) as buy_count from orders where uid_from = 7 and status = 1 group by uid order by created_at desc;
+-----+------------------+-----+----------+---------------------+-----------------+-----------+
| id  | sn               | uid | uid_from | created_at          | buy_count_money | buy_count |
+-----+------------------+-----+----------+---------------------+-----------------+-----------+
| 100 | 2019070554985351 |  19 |        7 | 2019-07-05 10:29:26 | 0.01            |         1 |
|  99 | 2019070597571025 |  17 |        7 | 2019-07-05 10:29:14 | 0.02            |         2 |
|  83 | 2019070197101102 |   7 |        7 | 2019-07-01 10:03:06 | 0.02            |         2 |
+-----+------------------+-----+----------+---------------------+-----------------+-----------+
3 rows in set

可以看到用戶id爲7的記錄和17的記錄操作時間都不能滿足需求。不是最後的操作時間

4.我的目的是取到uid爲7的最後一次操作時間,現在更換查詢,排序方式

mysql> select id,sn,uid,uid_from,created_at,max(created_at) as last_created_at,sum(money) as buy_count_money,count(*) as buy_count from orders where uid_from = 7 and status = 1 group by uid order by last_created_at desc;
+-----+------------------+-----+----------+---------------------+---------------------+-----------------+-----------+
| id  | sn               | uid | uid_from | created_at          | last_created_at     | buy_count_money | buy_count |
+-----+------------------+-----+----------+---------------------+---------------------+-----------------+-----------+
|  83 | 2019070197101102 |   7 |        7 | 2019-07-01 10:03:06 | 2019-07-05 14:28:38 | 0.02            |         2 |
|  99 | 2019070597571025 |  17 |        7 | 2019-07-05 10:29:14 | 2019-07-05 13:45:38 | 0.02            |         2 |
| 100 | 2019070554985351 |  19 |        7 | 2019-07-05 10:29:26 | 2019-07-05 10:29:26 | 0.01            |         1 |
+-----+------------------+-----+----------+---------------------+---------------------+-----------------+-----------+
3 rows in set

改動查詢和排序,用聚合函數max(created_at) as last_created_at 查到最後操作時間,再對重命名操作時間 last_created_at 進行order by 排序,既可以按照最後操作時間排序,又可以對group by進行count sum等聚合操作。得到想要的查詢結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章