MySQL:使用sqlalchemy从临时表更新一个表中的多列

时间:2018-10-10 03:34:01

标签: python mysql sqlalchemy

我尝试使用sqlalchemy通过temp表更新现有表。我尝试了两种方法。

我尝试的一种方法是Updating a table from another table with multiple columns in sqlalchemy

select_query = select([temp_table.c.Deletion,temp_table.c.CreatedDate]).where(and_(table._table__.c.ID == temp_table.c.ID,table.__table__.c.Seq == temp_table.c.Seq))

session.execute(table.__table__.update().values(Deletion=select_query.with_only_columns(temp_table.c.Deletion),CostCtrCode=select_query.with_only_columns(temp_table.c.CostCtrCode))
>>>'NotImplementedError: Operator 'getitem' is not supported on this expression'.

另一种方式是SQLAlchemy Correlated Update with Multiple Columns?

select_query = select([temp_table.c.Deletion,temp_table.c.CreatedDate]).where(and_(table._table__.c.ID == temp_table.c.ID,table.__table__.c.Seq == temp_table.c.Seq))

session.execute(table.__table__.update().values({tuple_(table.__table__.c.Deletion,table.__table__.c.CreatedDate).self_group(): select_query}))

>>>sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError)
("You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near '(`Deletion`, `CreatedDate`'
at line 1") [SQL: 'UPDATE `XXX` SET (`Deletion`, `CreatedDate`)=(SELECT `XXX_Temp`.   
`Deletion`, `XXX_Temp`.`CreatedDate` \nWHERE `XXX`.`ID` = `XXX_Temp`.`ID` AND `XXX`.
`Seq` = `XXX_Temp`.`Seq` )']

我该如何解决这个问题

0 个答案:

没有答案