ETL脚本使用INSERT INTO .... ON DUPLICATE KEY更新表

时间:2018-07-26 20:48:58

标签: python mysql

我已经在堆栈溢出中搜索了可能的解决方案,但是找不到。请不要将其标记为重复。

我正在处理ETL脚本,以对MySQL表执行UPSERT。

emp表具有主键(id,emp_name),现在我已将AUTO INCREMENT删除为列ID,因为当现有记录有更多UPDATE时,我们在ID号上存在巨大空白。

现在,我想提供自己的ID值,该值将是连续的(通过使用表中的MAX(ID))。我遇到了一些问题,因为MAX(ID)+1没有插入表中。请帮助解决此问题。

conn = mysql.connector.connect(host=url, user=uname, password=pwd, database=dbase)
cur = conn.cursor()

insertQry = "INSERT INTO emp (id, emp_name, dept, designation, address1, city, state, active_start_date, is_active) SELECT (SELECT coalesce(MAX(ID),0) + 1 FROM atlas.emp) id, tmp.emp_name, tmp.dept, tmp.designation, tmp.address1, tmp.city, tmp.state, tmp.active_start_date, tmp.is_active from EMP_STG tmp ON DUPLICATE KEY UPDATE dept=tmp.dept, designation=tmp.designation, address1=tmp.address1, city=tmp.city, state=tmp.state, active_start_date=tmp.active_start_date, is_active =tmp.is_active ;"

n = cur.execute(loadQry)
print (" CURSOR status :", n)

0 个答案:

没有答案
相关问题