亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql的group by 語法講義

發布時間:2020-04-30 12:00:29 來源:億速云 閱讀:259 作者:三月 欄目:MySQL數據庫

本文主要給大家介紹mysql的group by 語法講義,文章內容都是筆者用心摘選和編輯的,mysql的group by 語法講義具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下主題內容吧。

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

相關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 語法講義,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業知識信息 ,可以持續關注我們的行業資訊欄目的。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

郸城县| 和静县| 龙江县| 长汀县| 灵宝市| 彰武县| 西吉县| 长泰县| 永平县| 阜南县| 从江县| 西乡县| 霍山县| 宜章县| 淳化县| 涿鹿县| 九台市| 焦作市| 团风县| 泸水县| 安多县| 遵义市| 南溪县| 高唐县| 石首市| 固镇县| 营口市| 淳化县| 岑溪市| 徐州市| 德令哈市| 垫江县| 伊川县| 北宁市| 平凉市| 水城县| 山西省| 从江县| 报价| 阳朔县| 泊头市|