Python 重用psycopg2.execute中的参数
我有这样的SQL查询我知道这种情况可以优化,我的观点是使用具有相同参数的不同查询:Python 重用psycopg2.execute中的参数,python,parameters,psycopg2,Python,Parameters,Psycopg2,我有这样的SQL查询我知道这种情况可以优化,我的观点是使用具有相同参数的不同查询: select * from a where x > %s and y < %s union select * from b where x > %s and y < %s union select * from c where x > %s and y < %s union select * from d where x > %s and y < %s mycur
select * from a where x > %s and y < %s
union
select * from b where x > %s and y < %s
union
select * from c where x > %s and y < %s
union
select * from d where x > %s and y < %s
mycursor.execute(query_from_above, (since, to, since, to, since, to, since, to))
我想这样称呼它
mycursor.execute(query_from_above, (since, to))
是否可以以某种方式修改查询,以便使用较短版本的execute
编辑:
有解决此问题的方法:
也许是更好的:
你能试试这样的吗
mycursor.execute("""select * from a where x > %(since)s and y < %(to)s
union
select * from b where x > %(since)s and y < %(to)s
union
select * from c where x > %(since)s and y < %(to)s
union
select * from d where x > %(since)s and y < %(to)s""",
{'since': since, 'to': to}
)