Sunshine, Pure Land
动物的趋光性

今天在站上发现一错误,这样说吧:有一列表,我在第2页看到数据A,然后在第5页也看到数据A了!

我的分页语句是这样的:

select top 15 id from table1 where leibieid=1 and ish=1 and
id not in(select top 90 id from table1 where leibieid=1 and ish=1 order by Gsrz desc,time desc)
order by  Gsrz desc,time desc

将这句SQL分解成单纯的查询:

select id from table1 where leibieid=1 and ish=1 order by Gsrz desc,time desc

得到如下数据:

而我使用的分页SQL得到的结果却是:

大家可以看到两次查寻得到的数据是完全不一样的,

下面给大家看一组分析数据:

为了能更直接的找到原因,我将查寻语句微改了下,

select top 15 id,gsrz,time from jzw_gs_zxgs where leibieid=1 and ish=1 and
id not in(select top 0 id from jzw_gs_zxgs where leibieid=1 and ish=1 order by Gsrz desc,time desc)
order by Gsrz desc,time desc


相当于这句SQL在执行的时候先执行了NOT IN的语句,查找到15条满足该条件的数据后

再对这些数据进行新一轮的排序。很明显的:逻辑就不对了!

然后我将SQL换成:

 SELECT TOP 15 id, FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Gsrz desc,time desc) AS RowNumber,* FROM table1 where leibieid=1 and ish=1) AWHERE RowNumber > 15*(1-1)

测试结果:


与我想的结果完全是一样,但是效率却不好,然后我想到Top Max模式的分页方法,

但是很Top Max模式的分页方法难满足多列混合排序的情形,所以还是选择上一种方法

posted on 2011-03-04 15:21  冰-阳光  阅读(570)  评论(0编辑  收藏  举报