【SQLAlchemy】SQLAlchemy修改查询字段列名


SQLAlchemy问题记录

company    price    quantity
Microsoft  100      10
Google     99       5
Google     99       20
Google     101      15

 


要实现脚本
select price, sum(quantity) as num from shares where company='Google' group by price;

SQLAlchemy写法

你实际上需要label()方法。

result = dbsession.query(Shares.price, \
                            func.sum(Shares.quantity).label("Total sold")) \
                            .filter(Shares.company== 'Google') \
                            .group_by(Shares.price).all()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM