分组&在sqlalchemy中计数函数 [英] Group by & count function in sqlalchemy
本文介绍了分组&在sqlalchemy中计数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在sqlalchemy中使用group by and count命令。
I want a "group by and count" command in sqlalchemy. How can I do this?
推荐答案
dalloliogm的回答将工作,但它会计算组的数量,而不是每组中的行数。
The query in dalloliogm's answer will work, but it will count the number of groups, not the number of rows in each group.
计数文档说,对于 group_by
查询最好使用 func.count()
:
The documentation on counting says that for group_by
queries it is better to use func.count()
:
from sqlalchemy import func
session.query(Table.column, func.count(Table.column)).group_by(Table.column).all()
这篇关于分组&在sqlalchemy中计数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文