如果要实现:
选择table1表中存在“在tableid值比它的id值大1的记录”的记录
sql 语句怎么写? 我的思路大概是:
select * from table1 where exists (select * from table1 where id=id+1)
发现exists后面的括号里的where后面那里怎么写都不行 
要求:不能只给思路,要在SQL Server中运行通过

解决方案 »

  1.   

    select count(*) from table1 where tableid=id+1
      

  2.   


    select identity(int,1,1) id,v into #T
    from
    (
    select 1 v union all
    select 6 union all
    select 4 union all
    select 2 union all
    select 1 union all
    select 7 union all
    select 7 ) tselect * from #t where v=id+1
    id          v
    ----------- -----------
    3           4
    6           7(2 行受影响)
    drop table #t
      

  3.   


    Create table table1
    (
       id int
      ,name varchar(50)

    go
    insert table1 values (1,'a')
    insert table1 values (2,'b')
    insert table1 values (4,'c')
    insert table1 values (6,'d')
    insert table1 values (7,'e')
    go
    select * from table1 a where exists (select * from table1 where id=a.id +1) select * from table1 a where exists (select * from table1 where id=a.id -1) 
      

  4.   

    select ID from T a where exists (select * from T where a.ID +1=ID )
      

  5.   

    ---那就写不存在table id值比它的id值小的记录
    select
      *
    from
      table1 t
    where
      not exists(select 1 from table1 where id<=t.id)
      

  6.   


    我的思路大概是: 
    select * from table1 T where exists (select * from table1 where id=T.id+1) 
    发现exists后面的括号里的where后面那里怎么写都不行 
      

  7.   

    select * from table1 T 
    where exists (select * from table1 where id=T.id+1 OR T.ID=ID+1)
      

  8.   

    select * from table1  t where exists (select * from table1 where id=id+1) 
      

  9.   

    9楼改一下
    select * from table1  t where exists (select * from table1 where id=t.id+1)