简体   繁体   English

为什么我的 SQLite 代码不起作用? (CS50 2020 Pset 7 - 电影)

[英]Why does my SQLite code not work? (CS50 2020 Pset 7 - Movies)

I've made this SQLite code in the Movies exercise of Pset 7 from CS50 2020. It's the query number 12. The objective is to find all the movies in which both Helena Carter and Johnny Depp acted:我在 CS50 2020 的 Pset 7 的电影练习中制作了这个 SQLite 代码。它是查询编号 12。目标是找到 Helena Carter 和 Johnny Depp 演过的所有电影:

SELECT movies.title FROM people
JOIN stars ON stars.person_id = people.id
JOIN movies ON movies.id = stars.movie_id
WHERE movies.id IN (SELECT movies.id FROM movies WHERE name == "Helena Bonham Carter") AND movies.id IN (SELECT movies.id FROM movies WHERE name == "Johnny Depp");

But this code doesn't output anything.但是这段代码没有 output 任何东西。 So I got some hints in Inte.net and wrote another code, that works correctly:所以我在 Inte.net 中得到了一些提示并编写了另一个代码,它可以正常工作:

SELECT movies.title FROM people
JOIN stars ON stars.person_id = people.id
JOIN movies ON movies.id = stars.movie_id
WHERE movies.id IN (SELECT movies.id FROM movies WHERE name == "Helena Bonham Carter") AND movies.id IN (
SELECT movies.id FROM people
JOIN stars ON stars.person_id = people.id
JOIN movies ON movies.id = stars.movie_id
WHERE name == "Johnny Depp");

I'm still confused.我仍然很困惑。 If you take off the second condition of the first code, it works fine, and finds all the movies in which Helena Carter acted.如果去掉第一个代码的第二个条件,它就可以正常工作,并找到海伦娜·卡特出演的所有电影。 Why did I have to JOIN tables again in order to make a second condition for Johnny Depp?为什么我必须再次 JOIN 表才能为 Johnny Depp 设置第二个条件?

Because each people row has exactly one name.因为每一people都有一个名字。 Therefore this phrase因此这句话

SELECT movies.id FROM movies WHERE name == "Helena Bonham Carter") 
AND movies.id IN (SELECT movies.id FROM movies WHERE name == "Johnny Depp"

will never return a result.永远不会返回结果。 (As will anything that tries to select name="a" AND name="b" ) (任何尝试 select name="a" AND name="b"东西都一样)

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

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