inyfee
V2EX  ›  问与答

django 如何对查询结果分组并取出每个分组中的其中一行数据?

  •  
  •   inyfee · Jul 1, 2014 · 7175 views
    This topic created in 4360 days ago, the information mentioned may be changed or developed.
    有这样一组数据 :
    id | topic_id | count
    1 | 1 | 100
    2 | 1 | 200
    3 | 1 | 300
    4 | 2 | 400
    5 | 2 | 500
    6 | 2 | 600


    我的需求是根据 topic_id 进行分组,并 只 取出每个分组中 count 最大的那个。
    请问该 怎么做
    1 replies    2014-07-01 13:14:13 +08:00
    inyfee
        1
    inyfee  
    OP
       Jul 1, 2014
    发完帖之后找出解决方法了:
    test = Count.objects.all()
    test.query.group_by = ['topic_id']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5391 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:18 · PVG 09:18 · LAX 18:18 · JFK 21:18
    ♥ Do have faith in what you're doing.