考虑到MySQL中的评级列表中没有滚动NOR的情况下,平均评级为零。
考虑以下模式:考虑到MySQL中的评级列表中没有滚动NOR的情况下,平均评级为零。,mysql,join,average,Mysql,Join,Average,考虑以下模式: Student (RollNo int Not Null, Name varchar(20) Not Null, YearOfAdmission int Not Null, PRIMARY KEY(RollNo)) 现答覆如下: 列出所有电影的平均评分(包括多个评分实例)的学生 不同日期的电影)低于他/她的朋友对这些电影的平均评分 (包括在不同日期对电影进行分级的多个实例)。(输出格式:RollNo1, AverageRating1、RollNo2、AverageRating2
Student (RollNo int Not Null, Name varchar(20) Not Null, YearOfAdmission int Not Null,
PRIMARY KEY(RollNo))
现答覆如下:
列出所有电影的平均评分(包括多个评分实例)的学生
不同日期的电影)低于他/她的朋友对这些电影的平均评分
(包括在不同日期对电影进行分级的多个实例)。(输出格式:RollNo1,
AverageRating1、RollNo2、AverageRating2)
一个可能的答案是-
Select x.OwnRoll as RollNo1, l1.average as AverageRating1,
x.FriendRoll as RollNo2, l2.average as AverageRating2
from
(
Select * from Friend
union
(
Select f.FriendRoll, f.OwnRoll from Friend as f
)
order by OwnRoll
) as x,
(
Select r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
) as l1,
(
Select r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
) as l2
where l1.Rollno = x.OwnRoll and l2.Rollno = x.FriendRoll
and l1.average > l2.average ;
但是这个版本没有考虑从未看过电影的朋友,所以他们的平均评分是0</p>。
提前感谢您对问题及其答案所做的任何更新。如果没有卷号,请执行此小更新以将评分平均值捕获为0
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in (Select RollNo from Rating)
)
您甚至可以进一步了解Coalesce
类似的问题也被贴出来了
最终答案是
Select x.OwnRoll as RollNo1, l1.average as AverageRating1,
x.FriendRoll as RollNo2, l2.average as AverageRating2
from
(
Select * from Friend
union (Select f.FriendRoll, f.OwnRoll from Friend as f)
order by OwnRoll
) as x,
(
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in
(Select RollNo from Rating))
) as l1,
(
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in
(Select RollNo from Rating))
) as l2
where l1.Rollno = x.OwnRoll and l2.Rollno = x.FriendRoll
and l1.average > l2.average
试着一步一步地理解它!!否则你会迷失方向:)添加此问题的动机是为了得到一个比我刚刚发布的更好、更简单的解决方案
Select x.OwnRoll as RollNo1, l1.average as AverageRating1,
x.FriendRoll as RollNo2, l2.average as AverageRating2
from
(
Select * from Friend
union
(
Select f.FriendRoll, f.OwnRoll from Friend as f
)
order by OwnRoll
) as x,
(
Select r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
) as l1,
(
Select r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
) as l2
where l1.Rollno = x.OwnRoll and l2.Rollno = x.FriendRoll
and l1.average > l2.average ;
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in (Select RollNo from Rating)
)
Select x.OwnRoll as RollNo1, l1.average as AverageRating1,
x.FriendRoll as RollNo2, l2.average as AverageRating2
from
(
Select * from Friend
union (Select f.FriendRoll, f.OwnRoll from Friend as f)
order by OwnRoll
) as x,
(
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in
(Select RollNo from Rating))
) as l1,
(
Select distinct r.Rollno, avg(r.Rating) as average
from Rating as r
group by r.Rollno
union
(
Select RollNo, 0 as average
from Student
where Rollno not in
(Select RollNo from Rating))
) as l2
where l1.Rollno = x.OwnRoll and l2.Rollno = x.FriendRoll
and l1.average > l2.average