我想根據SQLAlchemy中另一個表的其他多列更新一個表的多個列。我在測試時使用SQLite,所以我不能使用`UPDATE table1 SET col = val WHERE table1.key == table2.key「語法。從sqlalchemy中的多列更新另一個表的表格
換句話說,我試圖創建這個排序更新詢問:
UPDATE table1
SET
col1 = (SELECT col1 FROM table2 WHERE table2.key == table1.key),
col2 = (SELECT col2 FROM table2 WHERE table2.key == table1.key)
在SQLAlchemy中:
select_query1 = select([table2.c.col1]).where(table1.c.key == table2.c.key)
select_query2 = select([table2.c.col2]).where(table1.c.key == table2.c.key)
session.execute(table.update().values(col1=select_query1, col2=select_query2))
只有我想要做的查詢,而不是隻有一次兩次,除非SQLite和MySQL的有足夠的智慧不是讓該查詢兩次
http://stackoverflow.com/questions/42613777/sqlalchemy-correlated-update-with-multiplecolumns –