V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
luckylion
V2EX  ›  MySQL

上一篇 下一篇排序

  •  
  •   luckylion · 2015-04-21 08:29:14 +08:00 · 2666 次点击
    这是一个创建于 3298 天前的主题,其中的信息可能已经有所发展或是发生改变。

    table: article
    prefix: id(auto_increment),title(varchar),sort(int)

    查找当前文章的上一篇 下一篇怎样写,查询排序是 order by sort desc,id desc

    3 条回复    2015-04-21 11:18:00 +08:00
    endoffight
        1
    endoffight  
       2015-04-21 08:48:28 +08:00 via Android   ❤️ 1
    数量不大的时候可以用临时表
    greyby
        2
    greyby  
       2015-04-21 08:57:32 +08:00   ❤️ 1
    子查询查询出ID,再关联 ```select * from article join (select id from article order by sort desc, id desc limit page_size offset page * page_size ) as ariticle_id using(id)```

    如果数据量很大且只需要上一页下一页翻可以参考 ```twitter timeline``` 使用的 ```[cursoring](https://dev.twitter.com/overview/api/cursoring)```
    SErHo
        3
    SErHo  
       2015-04-21 11:18:00 +08:00   ❤️ 1
    我是这样搞的,数据量大时不知道有问题没有:

    https://github.com/SerhoLiu/serholiu.com/blob/master/miniakio/libs/models.py#L101
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2239 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:16 · PVG 13:16 · LAX 22:16 · JFK 01:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.