zoukankan      html  css  js  c++  java
  • 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就是额外的返回值)。

    实验:

     实验用表(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

      错误消息如图:

      

  • 相关阅读:
    禅道技术官网
    mysql中获取一天、一周、一月时间数据的各种sql语句写法
    使用HttpFileServer自建下载服务器
    axure新手入门教程
    Mysql存储过程查询结果赋值到变量的方法
    mysql 数据表中查找、删除重复记录
    oracle中的sql%rowcount,sql%found、sql%notfound、sql%rowcount和sql%isopen
    存储过程中的when others then 和 raise
    Oracle的DBMS_OUTPUT.PUT_LINE用法及脚本批处理方法
    oracle 特殊符号
  • 原文地址:https://www.cnblogs.com/shuangnet/p/2975462.html
Copyright © 2011-2022 走看看