gpt4 book ai didi

mysql - 复杂查询 : find all users who rated at least 2 common movies rated by u1

转载 作者:行者123 更新时间:2023-11-29 01:57:00 25 4
gpt4 key购买 nike

所以我们有如下电影评分关系数据库:

  mysql> select * from ratings;
+-----+-----+------+
| uid | mid | rid |
+-----+-----+------+
| u1 | m1 | 4 |
| u1 | m2 | 3 |
| u2 | m2 | 5 |
| u2 | m3 | 4 |
| u3 | m1 | 4 |
| u3 | m2 | 5 |
| u3 | m3 | 2 |
+-----+-----+------+
7 rows in set (0.03 sec)

因此,我们想要找到对至少 2 部普通电影评分的所有用户,这些电影由特定用户“u1”评分。换句话说,如果用户“u1”对电影 m1、m2、m3 进行了评分,我们需要所有其他至少对电影 m1、m2、m3 进行评分的用户 ID。

所以在这种情况下,答案应该是 u3,因为 u3 对 m1、m2、m3 进行了评级,其中包括 2 部带有“u1”的常见电影。我尝试了以下语句:

mysql> SELECT * 
FROM ratings
WHERE mid IN (SELECT mid FROM ratings WHERE uid='u1');


+-----+-----+------+
| uid | mid | rid |
+-----+-----+------+
| u1 | m1 | 4 |
| u1 | m2 | 3 |
| u2 | m2 | 5 |
| u3 | m1 | 4 |
| u3 | m2 | 5 |
+-----+-----+------+
5 rows in set (0.00 sec)

因此,这为我提供了与用户 u1 一样的普通电影的所有评级。现在,我怎样才能只获得那些至少有 2 个与 u1 相同的条目?

最佳答案

SELECT R.uid from ratings R
JOIN
(SELECT distinct mid
FROM ratings
WHERE uid='u1') T
ON R.mid = T.mid
group by R.uid
having count(R.mid) = 2;

+-----+
| uid |
+-----+
| u1 |
| u3 |
+-----+
2 rows in set (0.15 sec)

关于mysql - 复杂查询 : find all users who rated at least 2 common movies rated by u1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com