-9

I have this table in my DB:

ID    userID  MovieID  Rank
1      1        1        9
2      1        2        9
3      1        3        9
4      2        1        9
5      2        2        10

and I want to query it to get mutual movies between 2 users which they rated the same.

for example, in this case, movieID = 1 is rated with '9' by both user 1 and user 2.

so I want to get in the result only the movie 1.

--UPDATE--

so I came up with this (thanks to @RJ1990)

SELECT        MovieID
FROM            LoverMovie
WHERE        (LoverID = 1) OR
                         (LoverID = 2)
GROUP BY MovieID
HAVING        (COUNT(*) > 1) AND (ABS(MAX(Rank) - MIN(Rank)) < 3)

Is this kind of query coule be written with EF (DbContext)?

4

2 回答 2

1

方法 :

从下面的查询中,您将获得具有多个相似评级的特定电影的MovieIDRateRate_Count

select MovieID,Rank,Count(*) Rate_Count
  from your_table
 group by MovieID,Rank
having Count(*) > 1
于 2013-07-08T12:20:00.400 回答
1

目前尚不清楚您尝试过什么以及遇到了什么问题。但是这个查询返回有多个投票的电影:

from m in db.Movies // assume you have Movies table
join mr in db.MovieRates 
     on m.ID equals mr.MovieID into g // and MovieRates table
where g.Count() > 1
select m

或者,如果您定义了导航属性:

db.Movies.Where(m => m.Rates.Count() > 1)

如果您还需要验证费率是否来自不同的用户,那么不要.Count() > 1使用.Select(x => x.UserID).Distinct().Count() > 1- 此条件从费率中选择不同的用户 ID。

于 2013-07-08T12:05:04.803 回答