講解mysql group by 群組內排序的方法

jacklove
發布: 2019-02-23 15:33:24
原創
5518 人瀏覽過

mysql的group by語法可以依照指定的規則將資料分組,分組就是將一個資料集分割成若干個小區域,然後再針對若干個小區域進行數據處理。本文將介紹mysql使用group by分組時,實作群組內排序的方法。

相關mysql影片教學推薦:《mysql教學

mysql的group by語法可以將資料分組,但是分組後的資料並不能進行組內排序。
例如一個評論表有多個使用者評論,需要取得每個使用者最後評論的內容。

建立測試資料表及資料

CREATE TABLE `comment` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `user_id` int(10) unsigned NOT NULL,  `content` varchar(200) NOT NULL,  `addtime` datetime NOT NULL,  `lastmodify` datetime NOT NULL,  PRIMARY KEY (`id`),  KEY `user_id` (`user_id`),  KEY `addtime` (`addtime`),  KEY `uid_addtime` (`user_id`,`addtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `comment` (`id`, `user_id`, `content`, `addtime`, `lastmodify`) VALUES(1, 1, '评论1', '2017-05-17 00:00:00', '2017-05-17 00:00:00'),
(2, 1, '评论2', '2017-05-17 00:00:01', '2017-05-17 00:00:01'),
(3, 2, '评论1', '2017-05-17 00:00:02', '2017-05-17 00:00:02'),
(4, 2, '评论2', '2017-05-17 00:00:03', '2017-05-17 00:00:03'),
(5, 3, '评论1', '2017-05-17 00:00:04', '2017-05-17 00:00:04'),
(6, 1, '评论3', '2017-05-17 00:00:05', '2017-05-17 00:00:05'),
(7, 4, '评论1', '2017-05-17 00:00:06', '2017-05-17 00:00:06'),
(8, 4, '评论2', '2017-05-17 00:00:07', '2017-05-17 00:00:07'),
(9, 4, '评论3', '2017-05-17 00:00:08', '2017-05-17 00:00:08'),
(10, 4, '评论4', '2017-05-17 00:00:09', '2017-05-17 00:00:09'),
(11, 3, '评论2', '2017-05-17 00:00:10', '2017-05-17 00:00:10');select * from comment;+----+---------+---------+---------------------+---------------------+| id | user_id | content | addtime             | lastmodify          |
+----+---------+---------+---------------------+---------------------+|  1 |       1 | 评论1   | 2017-05-17 00:00:00 | 2017-05-17 00:00:00 |
|  2 |       1 | 评论2   | 2017-05-17 00:00:01 | 2017-05-17 00:00:01 |
|  3 |       2 | 评论1   | 2017-05-17 00:00:02 | 2017-05-17 00:00:02 |
|  4 |       2 | 评论2   | 2017-05-17 00:00:03 | 2017-05-17 00:00:03 |
|  5 |       3 | 评论1   | 2017-05-17 00:00:04 | 2017-05-17 00:00:04 |
|  6 |       1 | 评论3   | 2017-05-17 00:00:05 | 2017-05-17 00:00:05 |
|  7 |       4 | 评论1   | 2017-05-17 00:00:06 | 2017-05-17 00:00:06 |
|  8 |       4 | 评论2   | 2017-05-17 00:00:07 | 2017-05-17 00:00:07 |
|  9 |       4 | 评论3   | 2017-05-17 00:00:08 | 2017-05-17 00:00:08 |
| 10 |       4 | 评论4   | 2017-05-17 00:00:09 | 2017-05-17 00:00:09 |
| 11 |       3 | 评论2   | 2017-05-17 00:00:10 | 2017-05-17 00:00:10 |
+----+---------+---------+---------------------+---------------------+
登入後複製

在comment表中,每位使用者最後評論的內容就是id為6 ,4,11,10的記錄。

使用group by查詢

select * from comment group by user_id;
+----+---------+---------+---------------------+---------------------+| id | user_id | content | addtime             | lastmodify          |
+----+---------+---------+---------------------+---------------------+|  1 |       1 | 评论1   | 2017-05-17 00:00:00 | 2017-05-17 00:00:00 |
|  3 |       2 | 评论1   | 2017-05-17 00:00:02 | 2017-05-17 00:00:02 |
|  5 |       3 | 评论1   | 2017-05-17 00:00:04 | 2017-05-17 00:00:04 ||  7 |       4 | 评论1   | 2017-05-17 00:00:06 | 2017-05-17 00:00:06 |
+----+---------+---------+---------------------+---------------------+
登入後複製

可以看到結果,分組後只會傳回分組內的第一個資料。因為group by語法沒有進行群組內排序的功能,只會依mysql預設的排序顯示。
如何才能對group by分組內的資料進行排序了,這個需要依照不同的需求處理。

1.id最大的,評論時間肯定最新

這種情況我們可以用id代替時間去搜尋並組內排序,使用max(id)就可以取得到每個分組中最大的評論id(即最新的評論)

select * from comment where id in(select max(id) from comment group by user_id) order by user_id;
+----+---------+---------+---------------------+---------------------+| id | user_id | content | addtime             | lastmodify          |
+----+---------+---------+---------------------+---------------------+|  6 |       1 | 评论3   | 2017-05-17 00:00:05 | 2017-05-17 00:00:05 |
|  4 |       2 | 评论2   | 2017-05-17 00:00:03 | 2017-05-17 00:00:03 |
| 11 |       3 | 评论2   | 2017-05-17 00:00:10 | 2017-05-17 00:00:10 || 10 |       4 | 评论4   | 2017-05-17 00:00:09 | 2017-05-17 00:00:09 |
+----+---------+---------+---------------------+---------------------+
登入後複製

2. id與評論時間沒有關係,id大的評論時間可能不是最新

這種情況我們就需要使用max(addtime)來獲取最新的評論,但因為不同使用者的評論時間有可能相同,因此也需要加多user_id這個條件去查詢。

重新建立測試資料

truncate table comment;INSERT INTO `comment` (`id`, `user_id`, `content`, `addtime`, `lastmodify`) VALUES(1, 1, '评论1', '2017-05-17 00:00:00', '2017-05-17 00:00:00'),
(2, 1, '评论2', '2017-05-17 00:10:01', '2017-05-17 00:10:01'),
(3, 2, '评论1', '2017-05-17 00:10:02', '2017-05-17 00:10:02'),
(4, 2, '评论2', '2017-05-17 00:00:03', '2017-05-17 00:00:03'),
(5, 3, '评论1', '2017-05-17 00:10:04', '2017-05-17 00:10:04'),
(6, 1, '评论3', '2017-05-17 00:00:05', '2017-05-17 00:00:05'),
(7, 4, '评论1', '2017-05-17 00:00:06', '2017-05-17 00:00:06'),
(8, 4, '评论2', '2017-05-17 00:10:07', '2017-05-17 00:10:07'),
(9, 4, '评论3', '2017-05-17 00:00:08', '2017-05-17 00:00:08'),
(10, 4, '评论4', '2017-05-17 00:00:09', '2017-05-17 00:00:09'),
(11, 3, '评论2', '2017-05-17 00:00:10', '2017-05-17 00:00:10');select * from comment;+----+---------+---------+---------------------+---------------------+| id | user_id | content | addtime             | lastmodify          |
+----+---------+---------+---------------------+---------------------+|  1 |       1 | 评论1   | 2017-05-17 00:00:00 | 2017-05-17 00:00:00 |
|  2 |       1 | 评论2   | 2017-05-17 00:10:01 | 2017-05-17 00:10:01 |
|  3 |       2 | 评论1   | 2017-05-17 00:10:02 | 2017-05-17 00:10:02 |
|  4 |       2 | 评论2   | 2017-05-17 00:00:03 | 2017-05-17 00:00:03 |
|  5 |       3 | 评论1   | 2017-05-17 00:10:04 | 2017-05-17 00:10:04 |
|  6 |       1 | 评论3   | 2017-05-17 00:00:05 | 2017-05-17 00:00:05 |
|  7 |       4 | 评论1   | 2017-05-17 00:00:06 | 2017-05-17 00:00:06 |
|  8 |       4 | 评论2   | 2017-05-17 00:10:07 | 2017-05-17 00:10:07 |
|  9 |       4 | 评论3   | 2017-05-17 00:00:08 | 2017-05-17 00:00:08 |
| 10 |       4 | 评论4   | 2017-05-17 00:00:09 | 2017-05-17 00:00:09 |
| 11 |       3 | 评论2   | 2017-05-17 00:00:10 | 2017-05-17 00:00:10 |
+----+---------+---------+---------------------+---------------------+
登入後複製

符合條件的應該是id為2,3,5,8的記錄

select a.* from comment as a right join (select user_id, max(addtime) as maxtime from comment where user_id is not null group by user_id) as b 
on a.user_id=b.user_id and a.addtime=b.maxtime order by a.user_id asc;+------+---------+---------+---------------------+---------------------+| id   | user_id | content | addtime             | lastmodify          |
+------+---------+---------+---------------------+---------------------+|    2 |       1 | 评论2   | 2017-05-17 00:10:01 | 2017-05-17 00:10:01 |
|    3 |       2 | 评论1   | 2017-05-17 00:10:02 | 2017-05-17 00:10:02 |
|    5 |       3 | 评论1   | 2017-05-17 00:10:04 | 2017-05-17 00:10:04 |
|    8 |       4 | 评论2   | 2017-05-17 00:10:07 | 2017-05-17 00:10:07 |
+------+---------+---------+---------------------+---------------------+
登入後複製

使用right join可以減少外層的資料集。
where user_id is not null 可以讓group by user_id時使用索引。

這篇文章解釋了mysql group by 群組內排序的方法 ,更多相關內容請關注php中文網。

相關推薦:

如何利用php 來反射API取得類別資訊

#詳解在mysql查詢時,offset過大影響效能的原因與最佳化方法

關於php使用正規去除寬高樣式的方法

##

以上是講解mysql group by 群組內排序的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板