Sql学习第三天——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就是额外的返回值)。
实验:
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
错误消息如图: