六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

通过SQL的select语句自动得到count语句和得到sql server 2005 的分页查询

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-1-27 04:59:17 | 显示全部楼层 |阅读模式
 
String sql = "select u.name from Users u order by u.id ";System.out.println(SqlServer2005PageHepler.getLimitString(sql, 1, 10));System.out.println(SqlCountHepler.getCountString(sql));sql = "select u.sex,count(1) c,max(u.id) t_max,min(u.name) as t_min from users u group by u.sex having count(1) > 10 order by count(u.sex)";System.out.println(SqlCountHepler.getCountString(sql));System.out.println(SqlCountHepler.getCountString(sql));sql = "select u.username,count(1) c,max(u.id) t_max from tplt_user u group by u.username having count(1) > 5 order by count(u.username) DESC";System.out.println(SqlServer2005PageHepler.getLimitString(sql,1, 10));System.out.println(SqlCountHepler.getCountString(sql)); 
 执行结果:
select * from (select ROW_NUMBER() OVER (order by u.id  ) AS row_num_ ,  u.name from Users u ) t where row_num_ > 1 and row_num_ <=  10select count (1) count_  from Users u select count(1) count_ from (select u.sex,count(1) c,max(u.id) t_max,min(u.name) as t_min from users u group by u.sex having count(1) > 10  ) t_select count(1) count_ from (select u.sex,count(1) c,max(u.id) t_max,min(u.name) as t_min from users u group by u.sex having count(1) > 10  ) t_select * from (select ROW_NUMBER() OVER (order by count(u.username) DESC ) AS row_num_ ,  u.username,count(1) c,max(u.id) t_max from tplt_user u group by u.username having count(1) > 5 ) t where row_num_ > 1 and row_num_ <=  10select count(1) count_ from (select u.username,count(1) c,max(u.id) t_max from tplt_user u group by u.username having count(1) > 5  ) t_ 
 
 
 不支持"UNION" 和“UNION ALL” 这样的结果合并语句,例如:
select * from dbo.[user]unionselect top 1 * from dbo.[user] order by age 
如果你需要得到正确的结果你需要这样写,如下:
 
select * from (select * from dbo.[user]union select top 1 * from dbo.[user] order by age) Q order by id 
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表