sql 取中间几条记录

sql 取中间几条记录,第1张

--从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)

SELECT TOP n-m+1 FROM Table WHERE (id NOT IN (SELECT TOP m-1 id FROM Table ))

--从TABLE表中取出第m到n条记录 (Exists版本)

SELECT TOP n-m+1 FROM TABLE AS a WHERE Not Exists

(Select From (Select Top m-1 From TABLE order by id) b Where bid=aid )

Order by id

--m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名

Select Top n-m+1 From Table

Where Id(Select Max(Id) From

(Select Top m-1 Id From Table Order By Id Asc) Temp)

-----------------------------------------------------------------------------------------------------------------------------------------

表pictures中有两个字段:id与title。id是自动编号的­

表中有5条记录:1--p1,2--p2,3--p3,4--p4,5--p5­

一、找到了一个小规律

string sqlstr = "select top 4 from pictures order by id desc "; //查询结果p5,p4,p3,p2---说明是整个表先进行排序,再进行查询的­

string sqlstr = "select top 3 from (select top 4 from pictures order by id desc) "; //-------p5,p4,p3­

string sqlstr = "select top 3 from (select top 4 from pictures order by id desc) order by id desc";//-------p5,p4,p3

string sqlstr = "select top 3 from (select top 4 from pictures order by id desc) order by id asc"; //-------p2,p3,p4

二、获取单条记录:­

假设表中一共有counts条记录,现在想要查询第n条记录,则sql语句应是:­

select top 1 from (select top (counts-n+1) from pictures order by id desc) order by id asc­

第三条记录:­

string sqlstr = "select top 1 from (select top 3 from pictures order by id desc) order by id asc";//-------p3­

三、获取表中多条连续的记录­

假设表中一共有counts条记录,现在想要查询第n到第m条的记录,则sql语句应是:­

select top (m-n+1) from (select top (counts-n+1) from pictures order by id desc) order by id asc­

获取第二到第四条记录:­

取n到m行

1

select top m from tablename where id not in (select top n id from tablename order by id asc/|desc/)

2

select top m into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表

set rowcount n --只取n条结果

select from 表变量 order by columnname desc

3

select top n from

(select top m from tablename order by columnname) a

order by columnname desc

4如果tablename里没有其他identity列,那么:

先生成一个序列,存储在一临时表中

select identity(int) id0, into #temp from tablename

取n到m条的语句为:

select from #temp where id0 > =n and id0 <= m

如果你在执行select identity(int) id0, into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:

exec sp_dboption 你的DB名字,'select into/bulkcopy',true

5如果表里有identity属性,那么简单:

select from tablename where identity_col between n and m

6SQL2005开始可以使用row_number() over()生成行号

;with cte as

(

select id0=row_number() over(order by id), from tablename

)

select from cte where id0 between n to m

字母M的ASCII码值:二进制表示为01001101、十进制为77,十六进制数表示为4D。

ASCII码简介:

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。

顶4楼,分析的很对,2、3楼的说法的确是sqlserver的写法。

附例子:

在ORACLE中实现SELECT

TOP

N

由于ORACLE不支持SELECT

TOP语句,所以在ORACLE中经常是用ORDER

BY跟ROWNUM的组合来实现SELECT

TOP

N的查询。

简单地说,实现方法如下所示:

SELECT

列名1...列名n

FROM

(SELECT

列名1...列名n

FROM

表名

ORDER

BY

列名1...列名n)

WHERE

ROWNUM

<=

N(抽出记录数)

ORDER

BY

ROWNUM

ASC

下面举个例子简单说明一下。

顾客表customer(id,name)有如下数据:

ID

NAME

01

first

02

Second

03

third

04

forth

05

fifth

06

sixth

07

seventh

08

eighth

09

ninth

10

tenth

11

last

则按NAME的字母顺抽出前三个顾客的SQL语句如下所示:

SELECT

FROM

(SELECT

FROM

CUSTOMER

ORDER

BY

NAME)

WHERE

ROWNUM

<=

3

ORDER

BY

ROWNUM

ASC

输出结果为:

ID

NAME

08

eighth

05

fifth

01

first

如果不管是数字还是字母,都是按数字、字母对应的ASCII码进行排序的。排序规则数字、大写字母、小写字母。

参照一下对照表:

ASCII 33 = !

ASCII 34 = "

ASCII 35 = #

ASCII 36 = $

ASCII 37 = %

ASCII 38 = &

ASCII 39 = '

ASCII 40 = (

ASCII 41 = )

ASCII 42 =

ASCII 43 = +

ASCII 44 = ,

ASCII 45 = -

ASCII 46 =

ASCII 47 = /

ASCII 48 = 0

ASCII 49 = 1

ASCII 50 = 2

ASCII 51 = 3

ASCII 52 = 4

ASCII 53 = 5

ASCII 54 = 6

ASCII 55 = 7

ASCII 56 = 8

ASCII 57 = 9

ASCII 58 = :

ASCII 59 = ;

ASCII 60 = <

ASCII 61 = =

ASCII 62 = >

ASCII 63 =

ASCII 64 = @

ASCII 65 = A

ASCII 66 = B

ASCII 67 = C

ASCII 68 = D

ASCII 69 = E

ASCII 70 = F

ASCII 71 = G

ASCII 72 = H

ASCII 73 = I

ASCII 74 = J

ASCII 75 = K

ASCII 76 = L

ASCII 77 = M

ASCII 78 = N

ASCII 79 = O

ASCII 80 = P

ASCII 81 = Q

ASCII 82 = R

ASCII 83 = S

ASCII 84 = T

ASCII 85 = U

ASCII 86 = V

ASCII 87 = W

ASCII 88 = X

ASCII 89 = Y

ASCII 90 = Z

ASCII 91 = [

ASCII 92 = \

ASCII 93 = ]

ASCII 94 = ^

ASCII 95 = _

ASCII 96 = `

ASCII 97 = a

ASCII 98 = b

ASCII 99 = c

ASCII 100 = d

ASCII 101 = e

ASCII 102 = f

ASCII 103 = g

ASCII 104 = h

ASCII 105 = i

ASCII 106 = j

ASCII 107 = k

ASCII 108 = l

ASCII 109 = m

ASCII 110 = n

ASCII 111 = o

ASCII 112 = p

ASCII 113 = q

ASCII 114 = r

ASCII 115 = s

ASCII 116 = t

ASCII 117 = u

ASCII 118 = v

ASCII 119 = w

ASCII 120 = x

ASCII 121 = y

ASCII 122 = z

ASCII 123 = {

ASCII 124 = |

ASCII 125 = }

ASCII 126 = ~

以上就是关于sql 取中间几条记录全部的内容,包括:sql 取中间几条记录、sql语句查询结果只取从第m条开始到第n条结束请问该怎么做、M的ASCII码值为多少等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:优选云

原文地址:https://54852.com/mama/1489669.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-11-29
下一篇2023-11-29

发表评论

登录后才能评论

评论列表(0条)

    保存