英文:
Select similar movies based on franchises
问题
以下是您要的翻译部分:
"这个查询有效,但我只是想知道这是否是正确的做法:
SELECT DISTINCT movie_id FROM movies_franchises WHERE franchise_id IN (SELECT franchise_id FROM movies_franchises WHERE movie_id = 23) AND movie_id != 23"
英文:
Let's say i have this table
id | movie_id | franchise_id |
---|---|---|
1 | 23 | 15 |
1 | 24 | 15 |
1 | 25 | 15 |
1 | 26 | 16 |
1 | 23 | 16 |
One movie can have multiple franchises, so i need to get all movies with the same franchises. Like if i want to get all similar movies for the movie with id 23, i should get 24 25 26.
This query works, but i'm just wondering if this is the right way of doing it
SELECT DISTINCT movie_id FROM movies_franchises WHERE franchise_id IN (SELECT franchise_id FROM movies_franchises WHERE movie_id = 23) AND movie_id != 23
答案1
得分: 0
以下是翻译好的部分:
您当前的查询是正确的,但您也可以使用联接来表示这个查询:
<!-- language: sql -->
SELECT m2.movie_id
FROM movies_franchises m1
INNER JOIN movies_franchises m2
ON m2.franchise_id = m1.franchise_id
WHERE m1.movie_id = 23 AND m2.movie_id <> 23;
英文:
Your current query is fine, but you could also express this using a join:
<!-- language: sql -->
SELECT m2.movie_id
FROM movies_franchises m1
INNER JOIN movies_franchises m2
ON m2.franchise_id = m1.franchise_id
WHERE m1.movie_id = 23 AND m2.movie_id <> 23;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论