处理 WHERE 子句中的空数组
Deal with empty arrays in WHERE clause
我正在尝试使用接收名为 date
:
的变量的函数来执行查询
def initial_query(date):
# Some code verifying the correctness of the date
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.country_iso = ANY(%(countries)s)
AND maf.date >= '{date}'
"""
return query.format(date=date)
在 main()
中,我按以下方式使用 initial_query(date)
函数:
cursor.execute(initial_query(starting_date), {'countries': countries, 'clients': client_ids,
'merchants': merchant_codes })
其中 cursor
是一个 psycopg2 游标,变量 countries, clients, merchants
在某些情况下(但不是全部)可以是空列表。如果我向查询传递一个空列表,我将不会获得任何结果,因为任何行
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.pais_iso = ANY(%(countries)s)
总是 return 错误。所以我想知道是否有一些方法可以使用 SQL 代码来修改 initial_query 中的查询,以避免这些行总是 returning false 因为任何列表传递给光标为空。
我解决了它添加一个布尔变量以对每个案例有两个不同的查询
def initial_query(date, is_segment=False):
# Some code verifying the correctness of the date
if is_segment:
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.date >= '{date}'
"""
else:
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND maf.country_iso = ANY(%(countries)s)
AND maf.date >= '{date}'
"""
return query.format(date=date)
我正在尝试使用接收名为 date
:
def initial_query(date):
# Some code verifying the correctness of the date
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.country_iso = ANY(%(countries)s)
AND maf.date >= '{date}'
"""
return query.format(date=date)
在 main()
中,我按以下方式使用 initial_query(date)
函数:
cursor.execute(initial_query(starting_date), {'countries': countries, 'clients': client_ids,
'merchants': merchant_codes })
其中 cursor
是一个 psycopg2 游标,变量 countries, clients, merchants
在某些情况下(但不是全部)可以是空列表。如果我向查询传递一个空列表,我将不会获得任何结果,因为任何行
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.pais_iso = ANY(%(countries)s)
总是 return 错误。所以我想知道是否有一些方法可以使用 SQL 代码来修改 initial_query 中的查询,以避免这些行总是 returning false 因为任何列表传递给光标为空。
我解决了它添加一个布尔变量以对每个案例有两个不同的查询
def initial_query(date, is_segment=False):
# Some code verifying the correctness of the date
if is_segment:
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND (maf.client_id = ANY(%(clients)s::bigint[]) OR maf.merchant_code = ANY(%(merchants)s::varchar[]))
AND maf.date >= '{date}'
"""
else:
query = """ SELECT some_fields
FROM tableA A
INNER JOIN tableB B on A.transaction_id = B.transaccion_id
WHERE A.action_id in (1,2,3)
AND A.test = false
AND maf.country_iso = ANY(%(countries)s)
AND maf.date >= '{date}'
"""
return query.format(date=date)