简体   繁体   English

MySQL - 对条件相同的多行进行分组

[英]MySQL - Grouping multiple rows where criteria is the same

I have a table of movie ratings that contains millions of rows containing userid's, movieid's and ratings. 我有一个电影评级表,其中包含数百万行包含userid,movieid和评级的行。

| userId | movieId | rating |
------------------------------
| 1      | 213     | 5      |
| 1      | 245     | 4      |
| 2      | 213     | 4      |
| 2      | 245     | 4      |
| 3      | 657     | 5      |
| 3      | 245     | 5      |

I'm trying to figure out a way of grouping together userId's that contain matching sets of movieId's. 我试图想出一种将userId分组在一起的方法,其中包含匹配的movieId集合。 Ideally I want the query to only find matches if they have at least 5 movieId's in common and if the rating is above 4, but I've simplified it for this example. 理想情况下,我希望查询只查找匹配项,如果它们共有至少5个movieId,并且如果评级高于4,但我已经为此示例简化了它。

In the instance above, userId 1 and 2 would be the only users that match as they both contain the same movieIds. 在上面的实例中,userId 1和2将是唯一匹配的用户,因为它们都包含相同的movieIds。 I need a statement that would essentially replicate this. 我需要一个基本上可以复制它的声明。 Thanks in advance for any help. 在此先感谢您的帮助。

You can perform a self-join on matching movies, filter out records with uninteresting ratings, group by user-pairs and then filter the resulting groups for only those that have at least the requisite number of matching records: 您可以在匹配的电影上执行自我加入,过滤掉具有不感兴趣的评级的记录,按用户对进行分组,然后仅为那些至少具有必要匹配记录数的组过滤结果组:

SELECT   a.userId, b.userId
FROM     myTable a JOIN myTable b USING (movieId)
WHERE    a.userId < b.userId
     AND a.rating > 4
     AND b.rating > 4
GROUP BY a.userId, b.userId
HAVING   COUNT(*) >= 5
select movieId, rating 
from tablename
group by movieId 
having count(userId) > 1 and rating > 4;

this gives me movieId 245 and rating 5, which should be correct according to your provided example data, have more than 1 userId and a rating greater than 4. 这给了我movieId 245和等级5,根据你提供的示例数据,它应该是正确的,有超过1个userId和大于4的等级。

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

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