【发布时间】:2021-06-07 01:42:54
【问题描述】:
我想检查 emp_id 和 date 是否已经存在于 bill_item 和 bill_item_temp 表中?如果不存在,那么我必须插入?
s = CONN.cursor()
query = """insert into bill_item (id,product_name,price,create_date,emp_id)
select %s,%s,%s,%s,%s where not exists ( select 1 from bill_item where emp_id = %s and
select 1 from bill_item_ref where emp_id = %s);"""
s.execute(query,(id,product_name,price,create_date,emp_id,));
表:bill_item
id product_name price create_date emp_id
1 rice 10 2021-03-09 10:10:10.231321 12345
2 tea 10 2021-03-09 08:10:10.231321 22345
表:bill_item_temp
id product_name price create_date emp_id
1 drinks 10 2021-03-09 10:10:10.231321 67345
2 snacks 10 2021-03-09 08:10:10.231321 92345
【问题讨论】:
-
与您的问题无关,但是:Postgres 9.1 是no longer supported,您应该尽快计划升级。
-
您能否提供一个替代解决方案,说明如何同时使用存在来检查 2 个选择查询
标签: python sql postgresql postgresql-9.1 exists