Python 3.x 索引器错误:将变量传递到SQL函数的python函数时,元组超出范围
我有一个SQL,它有两个子查询,如下所示。当我在SQL编辑器中运行它时,它运行得很好,但是如果我将它用作Python函数,我会得到一个错误Python 3.x 索引器错误:将变量传递到SQL函数的python函数时,元组超出范围,python-3.x,variables,amazon-redshift,Python 3.x,Variables,Amazon Redshift,我有一个SQL,它有两个子查询,如下所示。当我在SQL编辑器中运行它时,它运行得很好,但是如果我将它用作Python函数,我会得到一个错误 IndexError: tuple index out of range 谁能告诉我这件事哪里出了问题 dwh_cursor.execute(sql.SQL(""" with base as (select a.id,a.store, b.prod_id from sales a join product b on a.prod_id = b.id),
IndexError: tuple index out of range
谁能告诉我这件事哪里出了问题
dwh_cursor.execute(sql.SQL("""
with base as
(select a.id,a.store, b.prod_id
from sales a join product b on a.prod_id = b.id),
sub_query as
(select a.cust_name,b.id, b.prod_id
from customers a join product b on a.prod_id = b.id)
select base.store,base.prod_id,sub_query.cust_name
from base join sub_query on base.id = sub_query.id
and sub_query.cust_name = {}""").format(sql.Literal(name)))
我使用的是红移数据库。谢谢您正在创建两个表,然后在上一次查询中将它们一起查询 但是,在第二个查询中,您正在连接表a上的表b,而不使用“on”。相反,您使用的是“where”。尝试使用如下连接查询第二个表:
from customers a join product b on a.prod_id = b.id
而不是:
from customers a join product b where a.prod_id = b.id
谢谢你的回复。我在这里粘贴代码时拼写错误。我已经更新了代码。我在第二个子查询中使用“on”而不是“where”