Home > Database > Mysql Tutorial > body text

浅淡SqlServer的Top与Oracle的RowNum

WBOY
Release: 2016-06-07 15:51:38
Original
1114 people have browsed it

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 平时的项目开发中,分页存储过程是用的比较多的存储过程,SqlServer分页存储过程中经常要用到top,Oracle中则经常用到了RowNum. 现在,有一个UserInfo表,一个字段是UserId,另一个字段是UserName,其中

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

  平时的项目开发中,分页存储过程是用的比较多的存储过程,SqlServer分页存储过程中经常要用到top,Oracle中则经常用到了RowNum.

  现在,有一个UserInfo表,一个字段是UserId,另一个字段是UserName,其中是UserId是自动增长的,步长是1.表中共有30条数据,其中UserId的值不一定是连续的。现在要实现的目的是取其中的第11至第20条记录。先看SqlServer的几种做法:

  第一种写法:

  select top 10 *

  from UserInfo

  where UserId in

  (

  select top 20 UserId

  from UserInfo

  )

  order by UserId desc

  浅淡SqlServer的Top与Oracle的RowNum

  第二种写法:

  select top 10 * from UserInfo where UserId not in

  (select top 10 UserId from UserInfo )

  浅淡SqlServer的Top与Oracle的RowNum

  第三种写法:

  select top 10 * from UserInfo where UserId>

  (select max(UserId) from

  (select top10 UserId from UserInfo order by UserId) a)

  浅淡SqlServer的Top与Oracle的RowNum

  第四种写法(只可在Sqlserver 2005中):

  select * from (select Row_Number() over

  (Order by UserId) as RowId ,* from UserInfo) U

  where U.RowId between 10 and 20

  浅淡SqlServer的Top与Oracle的RowNum

[1] [2] 

浅淡SqlServer的Top与Oracle的RowNum

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template