从两个相同的表中选择相交

发布于 2024-08-14 18:37:57 字数 384 浏览 5 评论 0原文

查询:

SELECT id_user 
  FROM Rating 
 Where id_movie=2 
INTERSECT 
SELECT id_user 
  FROM Rating 
 Where id_movie=3

但我得到:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 'INTERSECT SELECT id_user FROM RatingWhere id_movie=3 LIMIT 0, 30' 附近使用的正确语法< /p>

语法有解决办法吗??

The query:

SELECT id_user 
  FROM Rating 
 Where id_movie=2 
INTERSECT 
SELECT id_user 
  FROM Rating 
 Where id_movie=3

but I get:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERSECT SELECT id_user FROM Rating Where id_movie=3 LIMIT 0, 30' at line 1

any have solution??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-08-21 18:37:57

以下查询将执行..

SELECT id_user 
FROM Rating 
Where id_movie=2 and id_user in 
             ( SELECT id_user FROM Rating Where id_movie=3);

Following query will do ..

SELECT id_user 
FROM Rating 
Where id_movie=2 and id_user in 
             ( SELECT id_user FROM Rating Where id_movie=3);
情深已缘浅 2024-08-21 18:37:57

MySql 中没有 Intersect、Minus 关键字,解决方法分别是

  1. Inner Join、
  2. Subqueries 或
  3. Left Join。

请查看这里

在 MySQL 中执行 INTERSECT 和 MINUS

我已经尝试过(虽然我是 SQL Server 人员)

输入:

id_user id_movie
101 1
102 2
102 3
104 4
102 5
107 6
102 2
103 3
109 9
110 2
110 3

使用相交(如果在 SQL Server 中运行)的输出将是

id_user
102
110

使用 Inner 的MySQL 兼容查询

查询 1 使用交叉连接

select distinct a.id_user
from Rating a
join Rating b on a.id_user = b.id_user
where a.id_movie  = 2 and b.id_movie  = 3

连接 查询2

select distinct a.id_user 
from Rating a, Rating b 
where a.id_user  = b.id_user 
  and a.id_movie  = 2
  and b.id_movie = 3

使用子查询的查询3

上面已经回答了。

Intersect, Minus keywords are absent in MySql and the workarounds are

  1. Inner Join, and
  2. Subqueries or
  3. Left Join respectively.

Please look into here

Doing INTERSECT and MINUS in MySQL

I have given a shot (though I am a SQL Server guy)

Input:

id_user id_movie
101 1
102 2
102 3
104 4
102 5
107 6
102 2
103 3
109 9
110 2
110 3

The output by using an intersect (if run in SQL Server) will be

id_user
102
110

MySQL compatible queries

Query 1 using Inner join

select distinct a.id_user
from Rating a
join Rating b on a.id_user = b.id_user
where a.id_movie  = 2 and b.id_movie  = 3

Query 2 using Cross join

select distinct a.id_user 
from Rating a, Rating b 
where a.id_user  = b.id_user 
  and a.id_movie  = 2
  and b.id_movie = 3

Query 3 using subquery

Already answered above.

蔚蓝源自深海 2024-08-21 18:37:57

怎么样:

SELECT r2.id_user
FROM Rating AS r2
   JOIN
   Rating AS r3
   ON r3.id_user = r2.id_user
   AND r2.id_movie=2
   AND r3.id_movie=3;

这里的想法是,您希望将“Rating”中的一行与“Rating”中的另一行连接起来,同一用户已看过电影 2 和 3。

How about:

SELECT r2.id_user
FROM Rating AS r2
   JOIN
   Rating AS r3
   ON r3.id_user = r2.id_user
   AND r2.id_movie=2
   AND r3.id_movie=3;

The idea here is that you want to join a row in Rating with another row in Rating, for which the same user has seen movies 2 and 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文