[b]关于with ties[/b]
对于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就是额外的返回值)。
[b]实验:[/b]
实验用表(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
结果图:
[img]http://files.jb51.net/file_images/article/201303/2013032215513395.png[/img]
[b]操作步骤1:不用with ties[/b]
代码:
select top 3 * from peopleinfo order by [name] desc
结果如图:
[img]http://files.jb51.net/file_images/article/201303/2013032215513396.png[/img]
[b]操作步骤2:用with ties[/b]
代码:
select top 3 with ties * from peopleinfo order by [name] desc
结果如图:
[img]http://files.jb51.net/file_images/article/201303/2013032215513397.png[/img]
[b]如果with ties不与top和order by结合使用的错误示范:[/b]
[b]操作步骤1:不与order by结合使用,只和top结合使用:[/b]
[b][/b]代码:
select top 3 with ties * from peopleinfo
错误消息如图:
[img]http://files.jb51.net/file_images/article/201303/2013032215513398.png[/img]
[b]操作步骤2:不与top结合使用,只和
order by[/b]结合使用:
[b][/b]代码:
select with ties * from peopleinfo order by [name] desc
错误消息如图:
[img]http://files.jb51.net/file_images/article/201303/2013032215513399.png[/img]
[b]操作步骤3:不与top结合使用
也不与order by[/b]结合使用:
[b][/b]代码:
select with ties * from peopleinfo
错误消息如图:
[img]http://files.jb51.net/file_images/article/201303/20130322155133100.png[/img]