debugksir
V2EX  ›  Django

Django 查询问题

  •  
  •   debugksir · Dec 31, 2019 · 3743 views
    This topic created in 2330 days ago, the information mentioned may be changed or developed.

    现有一张成绩表, 主键是用户 id, 如何根据用户 id 查询该用户在表中是否排在前 100 名以及上榜后的名次

    3 replies    2019-12-31 15:01:50 +08:00
    wingor2015
        1
    wingor2015  
       Dec 31, 2019
    for ind, item in enumerate(Score.objects.order_by('-grade').values('user_id', 'grade')[:100], 1):
    if item['user_id'] == user_id:
    return ind
    else:
    return -1
    georgema1982
        2
    georgema1982  
       Dec 31, 2019
    根据什么来排名?
    lenqu
        3
    lenqu  
       Dec 31, 2019
    表有么有索引呢?没索引小表直接全部载入,大表切割排序
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1019 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:28 · PVG 06:28 · LAX 15:28 · JFK 18:28
    ♥ Do have faith in what you're doing.