mysql中的AVG和ISNULL
我需要实现这样的目标:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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:
Full script as follows: