简体   繁体   English

计算数字的出现次数并在存储过程中使用 case 表达式

[英]Count occurrence of digits and using a case expression in a stored procedure

I want to write a stored procedure that displays the level of activity that an author has when it comes to create posts on a blog.我想编写一个存储过程来显示作者在博客上创建帖子时的活动级别。

I want to input a username in the call statement like call activity_level('Ugrob');我想在调用语句中输入用户名,例如call activity_level('Ugrob'); and get the following result并得到以下结果

nickname昵称 level_activity level_activity aupo_auth_id aupo_auth_id author_id author_id time_posting time_posting
Ugrob乌格罗布 high高的 1 1 1 1 2003-02-05 13:28:57 2003-02-05 13:28:57

I have the following tables我有以下表格

create table author_and_post(
aupo_post_id int,
aupo_auth_id int
);

create table author_info(
nickname varchar(16) primary key,
author_id int
);

create table posts(
post_id int,
time_posting datetime
);

Data for table author_and_post表 author_and_post 的数据

aupo_post_id aupo_post_id aupo_auth_id aupo_auth_id
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 5 1 1
6 6 1 1
7 7 2 2
8 8 2 2
9 9 2 2
10 10 3 3
11 11 3 3

data for table author_info表 author_info 的数据

nickname昵称 author_id author_id
Ugrob乌格罗布 1 1
Laeris莱里斯 2 2
Summona召唤兽 3 3

data for table posts表格帖子的数据

post_id post_id time_posting time_posting
1 1 2003-02-05 13:28:57 2003-02-05 13:28:57
2 2 2003-02-06 21:30:57 2003-02-06 21:30:57
3 3 2003-03-06 11:36:31 2003-03-06 11:36:31
4 4 2003-03-06 11:37:31 2003-03-06 11:37:31
5 5 2004-03-06 16:36:31 2004-03-06 16:36:31
6 6 2005-03-06 11:36:31 2005-03-06 11:36:31
7 7 2006-03-06 11:49:31 2006-03-06 11:49:31
8 8 2007-03-06 11:11:34 2007-03-06 11:11:34
9 9 2008-03-06 11:21:31 2008-03-06 11:21:31
10 10 2009-03-06 11:44:31 2009-03-06 11:44:31
11 11 2010-03-06 11:54:33 2010-03-06 11:54:33

I have written the following code我写了以下代码

drop procedure if exists level_activity; 
create procedure level_activity(user_insert varchar(16)) 
begin
 select nickname, count(*), aupo_auth_id, author_id, post_id, aupo_post_id, time_posting, 
 case
     when aupo_auth_id between 1 and 2 then 'low'
     when aupo_auth_id between 3 and 5 then 'medium'
     when aupo_auth_id > 5 then 'high' end as level_activity
from author_and_post join author_info 
on author_id = aupo_auth_id join posts on post_id = aupo_post_id
where nickname = user_insert
group by nickname, aupo_auth_id, author_id, post_id, aupo_post_id
end;
call activity_level('Lorry');

I'm getting我越来越

Error Code: 1064. You have an error in your SQL syntax;错误代码:1064。您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 12查看与您的 MySQL 服务器版本相对应的手册,了解在第 12 行的“结束”附近使用的正确语法

I wonder how to resolve this error and get the result I want.我想知道如何解决这个错误并得到我想要的结果。 I use MySQL version 8.0.23我用的是 MySQL 版本 8.0.23

Your GROUP BY clause needs to include every non-aggregated column in your SELECT clause.您的 GROUP BY 子句需要在 SELECT 子句中包含每个非聚合列。 It's missing time_posting and level_activity它缺少 time_posting 和 level_activity

There are couple of issues.有几个问题。 First, how did you decide on the time?首先,你是如何决定时间的? There are 6 timestamps and there are 6 posts for the author 'Ugrob'.作者“Ugrob”有 6 个时间戳和 6 个帖子。 Looking at your output it looks like max.看着你的 output 它看起来像最大值。 If so, you need to specify that in your select.如果是这样,您需要在 select 中指定。

Second, You need the count of the posts to determine level_activity and not the auth_id.其次,您需要帖子数来确定 level_activity 而不是 auth_id。 So you would add that to your case.因此,您会将其添加到您的案例中。

All the columns in your select should be part of the group by or aggregate function. select 中的所有列都应该是 group by 或聚合 function 的一部分。 So you need to remove post_id from your select.因此,您需要从 select 中删除 post_id。

Your select would look something like below.您的 select 如下所示。

 select nickname, aupo_auth_id, author_id, max(time_posting), count(aupo_post_id),
case 
     when count(aupo_post_id) between 1 and 2 then 'low'
     when count(aupo_post_id) between 3 and 5 then 'medium'
     else 'high' 
     end as level_activity
from author_and_post join author_info 
on author_id = aupo_auth_id join posts on post_id = aupo_post_id
where nickname = 'Ugrob'
group by nickname, aupo_auth_id, author_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM