登录
首页 >  数据库 >  MySQL

mysql-group by 组内排序

来源:SegmentFault

时间:2023-01-25 15:34:57 206浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《mysql-group by 组内排序》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

测试数据

CREATE TABLE `book_comment` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `like_num` int(10) NOT NULL DEFAULT '0' COMMENT '点赞数',
  `content` varchar(200) NOT NULL,
  `addtime` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `addtime` (`addtime`),
  KEY `uid_addtime` (`user_id`,`addtime`)
) ENGINE=InnoDB;


INSERT INTO `book_comment` VALUES ('1', '1', '0', '评论1', '2017-05-17 00:00:00');
INSERT INTO `book_comment` VALUES ('2', '1', '2', '评论2', '2017-05-17 00:03:01');
INSERT INTO `book_comment` VALUES ('3', '2', '3', '评论3', '2017-05-17 00:03:02');
INSERT INTO `book_comment` VALUES ('4', '2', '4', '评论4', '2017-05-17 00:00:03');
INSERT INTO `book_comment` VALUES ('5', '3', '0', '评论5', '2017-05-17 00:00:04');
INSERT INTO `book_comment` VALUES ('6', '1', '1', '评论6', '2017-05-17 00:00:05');
INSERT INTO `book_comment` VALUES ('7', '4', '0', '评论7', '2017-05-17 00:00:06');
INSERT INTO `book_comment` VALUES ('8', '4', '2', '评论8', '2017-05-17 00:00:07');
INSERT INTO `book_comment` VALUES ('9', '4', '0', '评论9', '2017-05-17 00:00:08');
INSERT INTO `book_comment` VALUES ('10', '4', '3', '评论10', '2017-05-17 00:00:09');
INSERT INTO `book_comment` VALUES ('11', '3', '2', '评论11', '2017-05-17 00:00:10');

1:常规group语句

mysql> SELECT *,count(*) as num from book_comment GROUP BY user_id;
+----+---------+----------+---------+---------------------+-----+
| id | user_id | like_num | content | addtime             | num |
+----+---------+----------+---------+---------------------+-----+
|  1 |       1 |        0 | 评论1   | 2017-05-17 00:00:00 |   3 |
|  3 |       2 |        3 | 评论3   | 2017-05-17 00:03:02 |   2 |
|  5 |       3 |        0 | 评论5   | 2017-05-17 00:00:04 |   2 |
|  7 |       4 |        0 | 评论7   | 2017-05-17 00:00:06 |   4 |
+----+---------+----------+---------+---------------------+-----+

根据用户分组,分组内默认显示第一条数据

2:显示分组中最新的数据

mysql> SELECT *,count(*) as num from book_comment GROUP BY user_id desc;
+----+---------+----------+---------+---------------------+-----+
| id | user_id | like_num | content | addtime             | num |
+----+---------+----------+---------+---------------------+-----+
| 10 |       4 |        3 | 评论10  | 2017-05-17 00:00:09 |   4 |
| 11 |       3 |        2 | 评论11  | 2017-05-17 00:00:10 |   2 |
|  4 |       2 |        4 | 评论4   | 2017-05-17 00:00:03 |   2 |
|  6 |       1 |        1 | 评论6   | 2017-05-17 00:00:05 |   3 |
+----+---------+----------+---------+---------------------+-----+

3:增加显示用户最大点赞数

mysql> SELECT *,count(*),max(like_num) as num from book_comment GROUP BY user_id desc;
+----+---------+----------+---------+---------------------+----------+-----+
| id | user_id | like_num | content | addtime             | count(*) | num |
+----+---------+----------+---------+---------------------+----------+-----+
| 10 |       4 |        3 | 评论10  | 2017-05-17 00:00:09 |        4 |   3 |
| 11 |       3 |        2 | 评论11  | 2017-05-17 00:00:10 |        2 |   2 |
|  4 |       2 |        4 | 评论4   | 2017-05-17 00:00:03 |        2 |   4 |
|  6 |       1 |        1 | 评论6   | 2017-05-17 00:00:05 |        3 |   2 |
+----+---------+----------+---------+---------------------+----------+-----+

4:获取不同用户点赞数最多的那一行

mysql> SELECT
    a.*
FROM
    book_comment AS a
INNER JOIN (
    SELECT
        user_id,
        max(like_num) AS like_num
    FROM
        book_comment
    GROUP BY
        user_id
) AS b ON a.user_id = b.user_id
AND a.like_num = b.like_num
ORDER BY
    a.user_id ASC;

+----+---------+----------+---------+---------------------+
| id | user_id | like_num | content | addtime             |
+----+---------+----------+---------+---------------------+
|  2 |       1 |        2 | 评论2   | 2017-05-17 00:03:01 |
|  4 |       2 |        4 | 评论4   | 2017-05-17 00:00:03 |
| 11 |       3 |        2 | 评论11  | 2017-05-17 00:00:10 |
| 10 |       4 |        3 | 评论10  | 2017-05-17 00:00:09 |
+----+---------+----------+---------+---------------------+

5: 其他分组排序方法

利用窗口函数功能(mysql8版本新增)完成分组排序功能

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表