源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Sql学习第三天——SQL 关于with ties介绍

  • 时间:2020-11-22 16:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Sql学习第三天——SQL 关于with ties介绍
[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):
[u]复制代码[/u] 代码如下:
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]
向表中插入数据:
[u]复制代码[/u] 代码如下:
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')
查看插入的全部数据:
[u]复制代码[/u] 代码如下:
select * from dbo.PeopleInfo
结果图: [img]http://files.jb51.net/file_images/article/201303/2013032215513395.png[/img] [b]操作步骤1:不用with ties[/b] 代码:
[u]复制代码[/u] 代码如下:
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] 代码:
[u]复制代码[/u] 代码如下:
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]代码:
[u]复制代码[/u] 代码如下:
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]代码:
[u]复制代码[/u] 代码如下:
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]代码:
[u]复制代码[/u] 代码如下:
select with ties * from peopleinfo
错误消息如图: [img]http://files.jb51.net/file_images/article/201303/20130322155133100.png[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部