推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

SQLAlchemy 中 query.filter(...)order_by(...).first() 换成 .second() 取第二条记录,最简单的方式是什么?

  •  
  •   miniyao · Nov 17, 2018 · 3412 views
    This topic created in 2744 days ago, the information mentioned may be changed or developed.
    query 取查询到的第一条记录,用.first()返回最简单。

    要是想直接获取排序中返回的第二条记录,用什么方式最简单?
    (我知道可以通过.all(),然后切片的方式,可以。显得查询量重了一点,最好有个什么简捷的方式,一步查出来,返回一条记录最简单)。
    6 replies    2018-11-19 09:14:05 +08:00
    fangdingjun
        1
    fangdingjun  
       Nov 17, 2018 via Android   ❤️ 1
    limit offset
    SingeeKing
        2
    SingeeKing  
    PRO
       Nov 17, 2018   ❤️ 1
    all() 是一个生成器,所以获取第二条并不会重……
    niubee1
        3
    niubee1  
       Nov 17, 2018   ❤️ 1
    query.filter(...)order_by(...)[1:2][0]
    sprite0616
        4
    sprite0616  
       Nov 18, 2018
    为什么不加个 filter,把第二条变成第一条呢。
    solu
        5
    solu  
       Nov 18, 2018 via iPhone
    limit 1,1
    vipppppp
        6
    vipppppp  
       Nov 19, 2018
    offset(1).limit(1) =>slice(1, 2)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2873 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 466ea39e · 44ms · UTC 12:08 · PVG 20:08 · LAX 05:08 · JFK 08:08
    ♥ Do have faith in what you're doing.