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

求一条sql语句????

//这条语句是正确的,可以实现分页

String sql = "select top "+pageSize+" * from loginUser where id not in(select top "+pageSize*(pageIndex-1)+" id from loginUser)";

//这条语句也是正确的,每次查询出10条语句,

String sql = "select top 10 * from loginUser where sex='男' and toupiaoTF=1 order by piaoshu desc";

我现在想实现将一两条语句合成一条语句,来实现分页查询的效果,应该是字查询吧,求各位高手解答 啊,小弟在此谢谢了啊

------解决方案--------------------
String sql = "select top "+pageSize+" * from loginUser where id not in(select top "+pageSize*(pageIndex-1)+" id from loginUser where sex='男' and toupiaoTF=1 order by piaoshu desc) and sex='男' and toupiaoTF=1 order by piaoshu desc";

------解决方案--------------------
其实本质上就是个“减法”,楼主你先理解这个概念,然后试试看吧:
Select Top :pageSize *
From loginUser 
Where id Not In (
Select Top (:pageIndex * (:pageSize - 1)) id
From loginUser 
Where sex='男' And toupiaoTF=1 
Order By piaoshu Desc
) And sex='男' And toupiaoTF=1 
Order By piaoshu Desc

注意 id 必须是主键。
------解决方案--------------------
探讨

为什么不根据表中的自增ID去实现分页呢?

------解决方案--------------------
我这儿没有SQLServer数据库,所以不好测试,建议你直接在SQLServer的管理器里面先试试看。

分段试一试,比如先试试看:
select top (5*(2-1)) id from loginUser where sex='女' and toupiaoTF=1 order by piaoshu desc

然后才全部加上:
select top 5 * from loginUser where id not in (
select top (5*(2-1)) id from loginUser where sex='女' and toupiaoTF=1 order by piaoshu desc
) and sex='女' and toupiaoTF=1 order by piaoshu desc