SQL 关于with ties
SQL 关于with ties
关于with ties
对于with ties一般是和Top , order by相结合使用的, 会查询出最后一条数据额外的返回值 (解释:如果按照order by 参数排序TOP n(PERCENT)返回了前面n(pencent)个记录,但是n+1…n+k条记录和排序后的第n条记录的参数值(order by 后面的参数)相同,则n+1、…、n+k也返回。n+1、…、n+k就是额外的返回值)。
实验:
实验用表( PeopleInfo ):
CREATE TABLE [ dbo ] . [ PeopleInfo ] ( [ id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL , [ name ] [ nchar ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL , [ numb ] [ nchar ] ( 10 ) COLLATE Chinese_PRC_CI_AS NOT NULL , [ phone ] [ nchar ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL ) ON [ PRIMARY ]
向表中插入数据:
insert into peopleinfo( [ name ] ,numb,phone) values ( ' 李欢 ' , ' 3223 ' , ' 1365255 ' ) insert into peopleinfo( [ name ] ,numb,phone) values ( ' 李欢 ' , ' 322123 ' , ' 1 ' ) insert into peopleinfo( [ name ] ,numb,phone) values ( ' 李名 ' , ' 3213112352 ' , ' 13152 ' ) insert into peopleinfo( [ name ] ,numb,phone) values ( ' 李名 ' , ' 32132312 ' , ' 13342563 ' )
查看插入的全部数据:
select * from dbo.PeopleInfo
结果图:
操作步骤1:不用with ties
代码:
select top 3 * from peopleinfo order by [ name ] desc
结果如图:
操作步骤2:用with ties
代码:
select top 3 with ties * from peopleinfo order by [ name ] desc
结果如图:
如果with ties不与top和order by结合使用的 错误示范 :
操作步骤1:不与order by结合使用,只和top结合使用:
代码:
select top 3 with ties * from peopleinfo
错误消息如图:
操作步骤2:不与top结合使用,只和 order by 结合使用:
代码:
select with ties * from peopleinfo order by [ name ] desc
错误消息如图:
操作步骤3:不与top结合使用 也不与order by 结合使用:
代码:
select with ties * from peopleinfo
错误消息如图:
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息