日期:2014-05-17  浏览次数:20561 次

sql分页语句写法

select top 2 * from firehonest_evaluate_activity where act_id 
  not in(select top 2 act_id from firehonest_evaluate_activity order by act_id asc) order by act_id asc
这个是在整张表中分页,如果需要有条件的分页,
比如

select * from firehonest_evaluate_activity where act_name like '%li%'


那整个分页语句怎么写?怎么把表名换成下面的select 语句?

------解决方案--------------------
select?top?2?*?from?firehonest_evaluate_activity?where?act_name like '%li%' and act_id?
??not?in(select?top?2?act_id?from?firehonest_evaluate_activity?order?by?act_id?asc)?order?by?act_id?asc
------解决方案--------------------
1. 用 row_number()产生行号
2. 再用 between xxx  and yyy  取从哪个序号到哪个序号

------解决方案--------------------
两种方式
1 版主的方式
2 2#的方式

第一种如果选择第几千页后速度超慢,资源消耗巨大,但是在前面的一些页时速度较快
第二种无论是哪页,资源消耗差别不大,在前一些页的查询消耗要比not in的方式差些,
但是几千几万页后优势明显,推荐第二种
------解决方案--------------------
try this,

select top 2 * 
from firehonest_evaluate_activity 
where act_name like '%li%' and 
act_id not in
(select top 2 act_id 
 from firehonest_evaluate_activity 
 where act_name like '%li%'
 order by act_id asc) 
order by act_id asc