使用sqlalchemy python查询表中的sum 2列

qpgpyjmq  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(229)

我是使用python3的新手我有桌上钱包:

ID name score1 score2
 1 name1    10     11
 2 name2     1      2
 3 name3     5      6

如何查询结果:

total_score = score1 + score 2

ID name total_score
 1 name1         21
 2 name2          3
 3 name3         11

然后我尝试将混合方法添加到钱包模型中:

@hybrid_method
def total_score(self, fields):
    return sum(getattr(self, field) for field in fields)

@total_score.expression
def total_score(cls, fields):
    return sum(getattr(cls, field) for field in fields)

以及:

wallets = Wallet.total_score(['socre1', 'score2']).label('total_score')

但没有什么能让钱包变化无常。我在使用代码:sqlalchemy(postgres+flask):如何对多个列求和?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题