如何通过Python在SQL中正确编写内部连接?

发布于 2025-02-13 09:31:17 字数 1105 浏览 1 评论 0原文

我有一个来自

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 技术交流群。

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

发布评论

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

评论(1

说谎友 2025-02-20 09:31:17

我在这里可以注意到的第一件事是您的查询跨越多行。为此,您使用了一次双引号(即,”)。但是,对于Python中的多条线字符串,您需要使用3次引号。
因此,您可以尝试使用类似的东西...

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;""" )

还有其他方法还可以编写跨越多行的字符串。其余的方式都在您身上找出答案!!!

是的,还需要引用查询内部的任何字符串字面的字符串(例如“导演”)。
(谢谢 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...

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;""" )

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)

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