zoukankan      html  css  js  c++  java
  • 查字段指定数据后一行记录

    /*
    ID    NUM 
    1    800 
    2    855 
    3    866 
    4    800 
    5    844 
    如何查NUM字段指定数据后一行记录?如NUM字段中800后一条记录
    */ 

    create table tb(ID int,   NUM int)
    insert into tb values(1 ,   800 )
    insert into tb values(2 ,   855 )
    insert into tb values(3 ,   866 )
    insert into tb values(4 ,   800 )
    insert into tb values(5 ,   844 )
    go

    --如果ID连续
    select m.* from tb m , tb n where m.id = n.id + 1 and n.num = 800
    /*

    ID          NUM         
    ----------- ----------- 
    2           855
    5           844

    (所影响的行数为 2 行)
    */

    --如果ID不连续,sql 2000
    select m.id , m.num from
    (
     
    select t.* , px = (select count(1) from tb where id < t.id) + 1 from tb t
    ) m,
    (
     
    select t.* , px = (select count(1) from tb where id < t.id) + 1 from tb t
    ) n
    where m.id = n.id + 1 and n.num = 800
    /*

    ID          NUM         
    ----------- ----------- 
    2           855
    5           844

    (所影响的行数为 2 行)
    */

    --如果ID不连续,sql 2005

    select m.id , m.num from
    (
     
    select t.* , px = row_number() over(order by id) from tb t
    ) m,
    (
     
    select t.* , px = row_number() over(order by id) from tb t
    ) n
    where m.id = n.id + 1 and n.num = 800
    /*

    ID          NUM         
    ----------- ----------- 
    2           855
    5           844

    (所影响的行数为 2 行)
    */

    drop table tb




  • 相关阅读:
    python_基础
    大话数据结构笔记
    c语言深度剖析
    liunx 第一章
    zookeper实现分布式锁
    zookeeper图形化操作工具
    windows下zookeeper集群的搭建
    TransactionInterceptor]: Bean property 'transactionManagerBeanName' is not w
    深入浅出zookeeper(二)
    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1638192.html
Copyright © 2011-2022 走看看