[MySQL]分页查询的三种方式
SQL中分页查询的方式,记录一下。方式有三种,各自特点就不说了。
CREATE TABLE 收藏表(
`id` bigint(20) unsigned NOT NULL auto_increment COMMENT 'primary key',
`uid` bigint(20) unsigned NOT NULL default 0 COMMENT 'uid',<br> `status` tinyint(3) unsigned NOT NULL default 0 COMMENT 'status',
`book_id` bigint(20) unsigned NOT NULL default 0 COMMENT 'book Id',
`create_time` int(11) unsigned not null default 0 COMMENT 'create time',
PRIMARY KEY (`id`),
UNIQUE KEY `uid_book_id` (`uid`, `book_id`),<br> KEY `uid_status` (`uid`, `status`)
)ENGINED=Innodb Auto_increment=1 default charset=gbk COMMENT '用户收藏信息';
最容易想到的第一种分页语句是(这也是我们最容易想到的语句):
select distinct uid from 收藏表 order by uid desc limit 0, 10;
select distinct uid from 收藏表 order by uid desc limit 11, 10;
再高级点语句,第二种($last_min_uid表示上一次读到的最后一个uid):
select distinct uid from 收藏表 order by uid desc limit 10;
select distinct uid from 收藏表 where uid < $last_min_uid order by uid desc limit 10;
最高级的方式
select uid from 收藏表 group by uid order by uid desc limit 10;
select uid from 收藏表 group by uid having uid < $last_min_uid order by uid desc limit 10;
标签: MySQL
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: