debugksir
V2EX  ›  Django

Django 查询问题

  •  
  •   debugksir · Dec 31, 2019 · 3763 views
    This topic created in 2342 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   ·   2929 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 08:13 · PVG 16:13 · LAX 01:13 · JFK 04:13
    ♥ Do have faith in what you're doing.