使用列表组合更新表列

时间:2019-04-11 05:56:27

标签: python sql

嗨,我想使用两个列表的组合来更新表的列。我尝试过但使用where子句,但是它首先完全检查列表,然后再检查另一个列表。我想使用组合来更新,例如1st value list1和1st value list2。


+------+----+----------+
| name | Id | ISACTIVE |
+------+----+----------+
| A    |  1 |        1 |
| B    |  2 |        1 |
| C    |  3 |        1 |
| A    |  4 |        1 |
| B    |  5 |        1 |
| C    |  6 |        1 |
+------+----+----------+

by default ISACTIVE is 1

name = ['A','B','C']
ID = [4,5,6]
placeholder= '?' # For SQLite. See DBAPI paramstyle.
placeholders= ', '.join(placeholder for unused in name)
placeholders_1 = ', '.join(placeholder for unused in ID)

allargs = NAME + ID
sql_query = """update prod_staging_db.outward_gstr1_forecast_data SET ISCURRENT = 0 where NAME NOT IN (%s) AND ID NOT IN (%s)"""%(placeholders, placeholders_1)
cursor.execute(sql_query,allargs)
cursor.commit()   

输出: 第二次运行添加了一些记录,然后更新了状态。所以输出应该像。 它应检查ID为4的A,然后将状态设置为1并保持为0,因此无论组合将其设置为1并始终保持为0

+------+----+----------+
| name | Id | ISACTIVE |
+------+----+----------+
| A    |  1 |        0 |
| B    |  2 |        0 |
| C    |  3 |        0 |
| A    |  4 |        1 |
| B    |  5 |        1 |
| C    |  6 |        1 |
+------+----+----------+

0 个答案:

没有答案