博客允许对其条目进行 comments .就我们这里的目的而言,我们只需要两张桌子,writercomment, 我们需要的所有字段是Writer.id、Writer.name、Comment.id、Comment.Author_id(外键)和Comment.Created(日期时间).

Question.是否有MySQL请求将输出每年最多产的三个 comments 作者的列表, 格式如下:三栏,第一栏是年份,第二栏是作者姓名,第三栏是数字 给定作者在给定年份发表的 comments . 因此,假设有足够的编写者和注释并且结果不相等,则输出中的行数应为 是博客存在年数的3倍(每年三行).

如果只要求提供一年的统计数据,则很容易做到以下几点:

SELECT YEAR(c.created), w.name, COUNT(c.id) as nbrOfComments
FROM comment AS c
INNER JOIN writer AS w
ON c.author_id = w.id 
WHERE YEAR(c.created)='2021'
GROUP BY w.id

出于测试目的,我在下面包含了一些用于创建和初始化表的MySQL代码.

CREATE TABLE  `writer` (
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(40) COLLATE utf8mb3_unicode_ci NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
CREATE TABLE  `comment` (
   `id` int NOT NULL AUTO_INCREMENT,
   `content` TEXT COLLATE utf8mb3_unicode_ci NOT NULL,
   `author_id` int NOT NULL,
   `created` datetime NOT NULL,
   PRIMARY KEY (`id`),
   FOREIGN KEY(author_id) REFERENCES writer(id)
 ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
INSERT INTO `writer` (`id`, `name`) VALUES 
 (1, 'Alf'),(2, 'Bob'),(3, 'Cathy'),(4, 'David'),
 (5, 'Eric'),(6, 'Fanny'),(7, 'Gabriel'),(8, 'Hans'),
 (9, 'Ibrahim'),(10, 'James'),(11, 'Kevin'),(12, 'Lena');
 
INSERT INTO `comment` (`id`, `author_id`, `content`, `created`) VALUES
(NULL, '1', 'some text here', '2021-01-22 06:40:31.000000'),
(NULL, '1', 'some text here', '2021-02-22 06:40:31.000000'),
(NULL, '1', 'some text here', '2021-03-22 06:40:31.000000'),
(NULL, '1', 'some text here', '2021-04-22 06:40:31.000000'),
(NULL, '2', 'some text here', '2021-01-22 06:40:31.000000'),
(NULL, '2', 'some text here', '2021-02-22 06:40:31.000000'),
(NULL, '2', 'some text here', '2021-03-22 06:40:31.000000'),
(NULL, '3', 'some text here', '2021-01-22 06:40:31.000000'),
(NULL, '3', 'some text here', '2021-02-22 06:40:31.000000'),
(NULL, '4', 'some text here', '2021-01-22 06:40:31.000000'),
(NULL, '5', 'some text here', '2022-01-22 06:40:31.000000'),
(NULL, '5', 'some text here', '2022-02-22 06:40:31.000000'),
(NULL, '5', 'some text here', '2022-03-22 06:40:31.000000'),
(NULL, '5', 'some text here', '2022-04-22 06:40:31.000000'),
(NULL, '6', 'some text here', '2022-01-22 06:40:31.000000'),
(NULL, '6', 'some text here', '2022-02-22 06:40:31.000000'),
(NULL, '6', 'some text here', '2022-03-22 06:40:31.000000'),
(NULL, '7', 'some text here', '2022-01-22 06:40:31.000000'),
(NULL, '7', 'some text here', '2022-02-22 06:40:31.000000'),
(NULL, '8', 'some text here', '2022-01-22 06:40:31.000000'), 
(NULL, '9', 'some text here', '2023-01-22 06:40:31.000000'),
(NULL, '9', 'some text here', '2023-02-22 06:40:31.000000'),
(NULL, '9', 'some text here', '2023-03-22 06:40:31.000000'),
(NULL, '9', 'some text here', '2023-04-22 06:40:31.000000'),
(NULL, '10', 'some text here', '2023-01-22 06:40:31.000000'),
(NULL, '10', 'some text here', '2023-02-22 06:40:31.000000'),
(NULL, '10', 'some text here', '2023-03-22 06:40:31.000000'),
(NULL, '11', 'some text here', '2023-01-22 06:40:31.000000'),
(NULL, '11', 'some text here', '2023-02-22 06:40:31.000000'),
(NULL, '12', 'some text here', '2023-01-22 06:40:31.000000');

推荐答案

如果你想要在过go 3年中每年 comments 排名前3的人,我建议你使用聚合和窗口函数:

select *
from (
    select year(c.created) year_created, w.name, count(*) as cnt_comments, 
        rank() over(partition by year(c.created) order by count(*) desc) rn
    from comment as c
    inner join writer as w
    on c.author_id = w.id 
    where year(c.created) >= year(current_date) - 2
    group by year(c.created), w.id
) c
where rn <= 3
order by year_created, cnt_comments desc

rank()为平局分配相同的索引,因此这might每年产生3行以上的行数.

对于您的样本数据,这将产生:

year_created name cnt_comments rn
2021 Alf 4 1
2021 Bob 3 2
2021 Cathy 2 3
2022 Eric 4 1
2022 Fanny 3 2
2022 Gabriel 2 3
2023 Ibrahim 4 1
2023 James 3 2
2023 Kevin 2 3

fiddle

Mysql相关问答推荐

在MySQL中从非连续数据创建时间线

在MySQL和JavaFX中保存和检索图像文件

获取最大登录应用程序用户数以及何时

Travis 构建失败并出现错误 LOAD DATA LOCAL INFILE 文件请求因访问限制而被拒绝

时间戳上滚动窗口的 SQL 计数不同

Select 不同的列,其中另一列不包含特定值

MySQL:根据条件查找某些用户的行位置

在 Codeigniter MySQL 中从另一个数据库获取数据

Django中的MYSQL查询等效

MySql中的可见索引和不可见索引是什么

使用带有 ELSEIF 和 ELSE 的 3 列更新问题

如何将表的链接列转换为 SQL 中的行?

MySql - 默认情况下主键是唯一的吗?

默认为空字符串的列

从mysql中的大表中快速 Select 随机行

在 Ubuntu 上安装 mysql gem 的困难

如何在执行 sql 脚本时 echo 打印语句

数据截断:第 1 行的logo列数据太长

MySQL 1062 - 键 'PRIMARY' 的重复条目 '0'

在 MySQL 的 LIMIT 子句中使用变量