select id from 表 where 条件 order by id asc
我已经知道id=5在查询结果里,我如何知道id=5在检索结果的第几个?比如如果检索结果为1 5 8 9,那么id=5在检索结果的第2个.这个查询该如何写,谢谢!!!
我已经知道id=5在查询结果里,我如何知道id=5在检索结果的第几个?比如如果检索结果为1 5 8 9,那么id=5在检索结果的第2个.这个查询该如何写,谢谢!!!
(select id from 表 where 条件 order by id asc) A
Where id <= 5
select * from #temp where 条件
where id=5
改下
select * into # from
(
select id from 表 where 条件 order by id asc
)aselect id,名次=(select count(1)+1 from # where id<a.id) from # a
where id=5
--number的值就表示id=5在检索结果的第几个。
select number from #tmp where id=5
drop table #tmp
go