python - 使用sqlalchemy python查询表中的sum 2列
问题描述
我是使用 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 添加到 Wallet 模型:
@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):如何对多列求和?
解决方案
推荐阅读
- python - 如何查看为什么 keras / tensorflow 模型会卡住?
- python - Conda 卸载无名包
- angular - Angular 应用程序编译正确,但未加载
- plugins - 无法将参数从命令传递到转换器
- web - 通过scrapy抓取多个链接
- java - 如何从一种方法调用变量/或从中创建字段变量
- angular - 在 Angular 7 http get 中看不到来自 ASP.NET Core MVC 的对象值
- c++ - std::iterator 是否可以检查下一个元素是否存在于该迭代器上?(此设计受家庭作业限制)
- python - pyinstaller错误启动服务:服务没有及时响应启动或控制请求
- android-studio - android studio 同步项目时会发生什么?