• 查字段指定数据后一行记录


    /*
    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




  • 相关阅读:
    background-clip与background-origin
    jquery判断一个元素是否为某元素的子元素
    Math.pow()实现开任意次方根
    vue基础点
    css3
    css系统学习
    angularJs
    jquery与JavaScript
    bootstrapt使用
    bootstrap
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1638192.html
Copyright © 2020-2023  润新知