Sql 用top not in分页问题
数据是SQlserver2008有一个表为AlertmessageLog字段id自动编号DeviceIdnvarcharalertmessagelognvarcharco...
数据是SQl server 2008
有一个表为AlertmessageLog
字段
id 自动编号
DeviceId nvarchar
alertmessagelog nvarchar
comid nvarchar
changetime datatime
这里面存放一百多条数据(有重复)!
id 自动编号里面无重复数据
DeviceId 和comid 有重复数据,但不依靠这两个字段去重复数据!
Alertmessagelog 里面有重复值,去除这个字段的重复数据并显示结果,返回查询结果中要含这个表中全部字段
用以下两种语句均可实现!
1 。select * from AlertmessageLog a
where not exists(select 1 from AlertmessageLog where alertmessagelog=a.alertmessagelog and id<a.id)
2. select * from AlertmessageLog A
where id=(select min(id) from AlertmessageLog where alertmessagelog=A.alertmessagelog )
我想用top 和not in 实现分页
如何把select top 2 * from alertMessageLog where id not in(select top 3 id from alertMessageLog)
分页的效果整合到上面查询语句中?我试了几次,但查询结果都是一样的!希望帮我写一下,任意一条都可以,谢谢………… 展开
有一个表为AlertmessageLog
字段
id 自动编号
DeviceId nvarchar
alertmessagelog nvarchar
comid nvarchar
changetime datatime
这里面存放一百多条数据(有重复)!
id 自动编号里面无重复数据
DeviceId 和comid 有重复数据,但不依靠这两个字段去重复数据!
Alertmessagelog 里面有重复值,去除这个字段的重复数据并显示结果,返回查询结果中要含这个表中全部字段
用以下两种语句均可实现!
1 。select * from AlertmessageLog a
where not exists(select 1 from AlertmessageLog where alertmessagelog=a.alertmessagelog and id<a.id)
2. select * from AlertmessageLog A
where id=(select min(id) from AlertmessageLog where alertmessagelog=A.alertmessagelog )
我想用top 和not in 实现分页
如何把select top 2 * from alertMessageLog where id not in(select top 3 id from alertMessageLog)
分页的效果整合到上面查询语句中?我试了几次,但查询结果都是一样的!希望帮我写一下,任意一条都可以,谢谢………… 展开
3个回答
2013-04-17
展开全部
这个是你想要的吗?
SELECT top 2 * FROM (select * from AlertmessageLogas A where id=(select max(id) from AlertmessageLog where AlertmessageLog=A.AlertmessageLog)) AS A WHERE id NOT IN ( SELECT top (2*2) id FROM (select * from AlertmessageLogas A where id=(select max(id) from AlertmessageLog where AlertmessageLog=A.AlertmessageLog)) as A)
SELECT top 2 * FROM (select * from AlertmessageLogas A where id=(select max(id) from AlertmessageLog where AlertmessageLog=A.AlertmessageLog)) AS A WHERE id NOT IN ( SELECT top (2*2) id FROM (select * from AlertmessageLogas A where id=(select max(id) from AlertmessageLog where AlertmessageLog=A.AlertmessageLog)) as A)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select top 2 * from AlertmessageLog A
where id=(select min(id) from AlertmessageLog where alertmessagelog=A.alertmessagelog )
这样不就行了吗?
where id=(select min(id) from AlertmessageLog where alertmessagelog=A.alertmessagelog )
这样不就行了吗?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-04-17
展开全部
select top 2 * from alertMessageLog where (id not in(select top 3 id from alertMessageLog) And id=(select min(id) from AlertmessageLog as A where alertmessagelog=A.alertmessagelog ))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询