zoukankan      html  css  js  c++  java
  • Question[SQL]: How can I list noncontignous data?

    Question: How can I list non-contignous data?
    In database pubs, I create a table test using statement as below, and I insert several row as below

     程序代码
    create table test
    ( id int primary key )
    go

    insert into test values (1 )
    insert into test values (2 )
    insert into test values (3 )
    insert into test values (4 )
    insert into test values (5 )
    insert into test values (6 )
    insert into test values (8 )
    insert into test values (9 )
    insert into test values (11)
    insert into test values (12)
    insert into test values (13)
    insert into test values (14)
    insert into test values (18)
    insert into test values (19)
    go


    Now I want to list the result of the non-contignous row as below,how can I do it?

     程序代码
    Missing after Missing before
    ------------- --------------
    6             8
    9             11


        if(OBJECT_ID('test'is not null)
            
    drop table test
        
    create table test
        (
            
    [Id] int primary key
        )
        
    insert into test values (1 )
        
    insert into test values (2 )
        
    insert into test values (3 )
        
    insert into test values (4 )
        
    insert into test values (5 )
        
    insert into test values (6 )
        
    insert into test values (8 )
        
    insert into test values (9 )
        
    insert into test values (11)
        
    insert into test values (12)
        
    insert into test values (13)
        
    insert into test values (14)
        
    insert into test values (18)
        
    insert into test values (19)
        
        
    select Id from test t where  not exists(select 1 from test where Id=t.Id+1
            
    or not exists(select 1 from test where Id=t.Id-1)
            
        
    drop table test

    Answer:


  • 相关阅读:
    HDU 5311
    HDU 1708
    HDU 1707
    计蒜之道 430
    Codeforces Round #292 (Div. 2)——C——Drazil and Factorial
    Codeforces Round #292 (Div. 2)——B——Drazil and His Happy Friends
    Codeforces Round #292 (Div. 2)——A——Drazil and Date
    Codeforces Round #293 (Div. 2)——C——Anya and Smartphone
    Codeforces Round #293 (Div. 2)——B——Tanya and Postcard
    Codeforces Round #293 (Div. 2)——A—— Vitaly and Strings
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760184.html
Copyright © 2011-2022 走看看