【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