mysql中的AVG和ISNULL

发布于 2024-10-19 16:20:38 字数 312 浏览 2 评论 0原文

我需要实现这样的目标:

SELECT ISNULL(AVG(rating),0) FROM videorating vr WHERE vr.VideoId=11229;

如果平均值为空/空,那么我应该得到 0 作为评级。

我正在尝试优化这个查询

SELECT CASE ISNULL(AVG(rating)) WHEN 0 THEN AVG(rating) ELSE 0 END AS rating FROM videorating WHERE videoID=11229;

I need to achieve some thing like this

SELECT ISNULL(AVG(rating),0) FROM videorating vr WHERE vr.VideoId=11229;

If average is empty/null then I should get 0 as rating.

I am trying to optimize this query

SELECT CASE ISNULL(AVG(rating)) WHEN 0 THEN AVG(rating) ELSE 0 END AS rating FROM videorating WHERE videoID=11229;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-10-26 16:20:38

就我个人而言,我会使用触发器来保持平均评级,因为随着数据库大小的增长,它会更加有效 - 然后你可以简单地执行以下操作:

select rating from video where video_id = 1;

完整脚本如下:

drop table if exists video;
create table video
(
video_id int unsigned not null auto_increment primary key,
title varchar(255) not null,
num_votes int unsigned not null default 0,
total_score int unsigned not null default 0,
rating decimal(8,2) not null default 0
)
engine = innodb;

drop table if exists video_vote;
create table video_vote
(
video_id int unsigned not null,
user_id int unsigned not null,
score tinyint unsigned not null default 0, -- 0 to 5
primary key (video_id, user_id)
)
engine=innodb;

delimiter #

create trigger video_vote_after_ins_trig after insert on video_vote
for each row
begin
 update video set 
    num_votes = num_votes + 1,
    total_score = total_score + new.score,
    rating = total_score / num_votes  
 where 
    video_id = new.video_id;
end#

delimiter ;

insert into video (title) values ('video 1'),('video 2'), ('video 3');

insert into video_vote (video_id, user_id, score) values
(1,1,5),(1,2,4),(1,3,3),(1,4,2),(1,5,1),
(2,1,2),(2,2,1),(2,3,4),
(3,1,4),(3,5,2);

select rating from video where video_id = 1;

Personally I'd use a trigger to keep an average rating as it will be much more efficient as your database grows in size - then you can simply do:

select rating from video where video_id = 1;

Full script as follows:

drop table if exists video;
create table video
(
video_id int unsigned not null auto_increment primary key,
title varchar(255) not null,
num_votes int unsigned not null default 0,
total_score int unsigned not null default 0,
rating decimal(8,2) not null default 0
)
engine = innodb;

drop table if exists video_vote;
create table video_vote
(
video_id int unsigned not null,
user_id int unsigned not null,
score tinyint unsigned not null default 0, -- 0 to 5
primary key (video_id, user_id)
)
engine=innodb;

delimiter #

create trigger video_vote_after_ins_trig after insert on video_vote
for each row
begin
 update video set 
    num_votes = num_votes + 1,
    total_score = total_score + new.score,
    rating = total_score / num_votes  
 where 
    video_id = new.video_id;
end#

delimiter ;

insert into video (title) values ('video 1'),('video 2'), ('video 3');

insert into video_vote (video_id, user_id, score) values
(1,1,5),(1,2,4),(1,3,3),(1,4,2),(1,5,1),
(2,1,2),(2,2,1),(2,3,4),
(3,1,4),(3,5,2);

select rating from video where video_id = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文