如何通过Python在SQL中正确编写内部连接?
我有一个来自
Crew : 'title_id', 'person_id', category, 'job', 'characters'
[('tt0000060', 'nm0005690', 'director', None, '\\N'),
('tt0000060', 'nm0005658', 'cinematographer', None, '\\N'),
('tt0000361', 'nm0349785', 'director', None, '\\N'),
('tt0000376', 'nm0466448', 'actor', None, '\\N'),
('tt0000376', 'nm0617272', 'actress', None, '["Salome"]')...
Ratings: 'title_id', 'rating', 'votes'
[('tt0000060', 7.8, 59),
('tt0000361', 8.1, 10),
('tt0000376', 7.8, 6),
('tt0000417', 8.2, 38836),
('tt0000505', 8.1, 11),
('tt0000738', 7.8, 11)...
我的代码是:
import sqlalchemy
from sqlalchemy import create_engine, text, inspect
engine = create_engine('sqlite:///newIMDB.db')
inspector = inspect(engine)
print(inspector.get_table_names()) #['crew', 'episodes', 'people', 'ratings', 'titles']
conn = engine.connect()
stmt = text ("SELECT category,rating FROM(SELECT * FROM crew INNER JOIN ratings
ON crew.title_id = ratings.title_id)
WHERE category=director AND rating > 9 LIMIT 10;" )
result = conn.execute(stmt)
result.fetchall()
我的错误在哪里?
I have a database from https://www.imdb.com. I would like to find which directors (if any) have obtained a score higher than 9 for all their titles?
Crew : 'title_id', 'person_id', category, 'job', 'characters'
[('tt0000060', 'nm0005690', 'director', None, '\\N'),
('tt0000060', 'nm0005658', 'cinematographer', None, '\\N'),
('tt0000361', 'nm0349785', 'director', None, '\\N'),
('tt0000376', 'nm0466448', 'actor', None, '\\N'),
('tt0000376', 'nm0617272', 'actress', None, '["Salome"]')...
Ratings: 'title_id', 'rating', 'votes'
[('tt0000060', 7.8, 59),
('tt0000361', 8.1, 10),
('tt0000376', 7.8, 6),
('tt0000417', 8.2, 38836),
('tt0000505', 8.1, 11),
('tt0000738', 7.8, 11)...
My code is:
import sqlalchemy
from sqlalchemy import create_engine, text, inspect
engine = create_engine('sqlite:///newIMDB.db')
inspector = inspect(engine)
print(inspector.get_table_names()) #['crew', 'episodes', 'people', 'ratings', 'titles']
conn = engine.connect()
stmt = text ("SELECT category,rating FROM(SELECT * FROM crew INNER JOIN ratings
ON crew.title_id = ratings.title_id)
WHERE category=director AND rating > 9 LIMIT 10;" )
result = conn.execute(stmt)
result.fetchall()
Where is my error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里可以注意到的第一件事是您的查询跨越多行。为此,您使用了一次双引号(即,”)。但是,对于Python中的多条线字符串,您需要使用3次引号。
因此,您可以尝试使用类似的东西...
还有其他方法还可以编写跨越多行的字符串。其余的方式都在您身上找出答案!!!
是的,还需要引用查询内部的任何字符串字面的字符串(例如“导演”)。
(谢谢 slothrop 用于指出这一点)
First thing I can notice here is your query is spanning across multiple lines. And for that you have used one time double quotes (i.e., "). But for multiple line strings in python you need to use 3 times quotes.
So can you try with something like...
There are also other ways to write strings spanning multiple lines as well. Rest of the ways are on you to find out!!!
Yes also any string literal(Like 'director') inside the query needs to be quoted.
(Thank you slothrop for pointing this out)