Django equivalent of COUNT with GROUP BY

前端 未结 2 772
耶瑟儿~
耶瑟儿~ 2020-12-07 16:00

I know Django 1.1 has some new aggregation methods. However I couldn\'t figure out equivalent of the following query:

SELECT player_type, COUNT(*) FROM playe         


        
2条回答
  •  遥遥无期
    2020-12-07 16:32

    If you are using Django 1.1 beta (trunk):

    Player.objects.values('player_type').order_by().annotate(Count('player_type'))
    
    • values('player_type') - for inclusion only player_type field into GROUP BY clause.
    • order_by() - for exclusion possible default ordering that can cause not needed fields inclusion in SELECT and GROUP BY.

提交回复
热议问题