Id      stu_name      stu_sex   
    1        王强          男          
    3        李娜          女 
    4        张峰          男
    6        钟柳          女  
例如我选中李娜得到李娜的id     我如何可以得到查询到王强 的id        

解决方案 »

  1.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([Id] int,[stu_name] nvarchar(2),[stu_sex] nvarchar(1))
    Insert tb
    select 1,N'王强',N'男' union all
    select 3,N'李娜',N'女' union all
    select 4,N'张峰',N'男' union all
    select 6,N'钟柳',N'女'
    Go
    ---如果ID递增
    select top 1 ID
    from tb 
    where id<(select top 1 ID 
              from tb
              where stu_name=N'李娜')
    order by id desc
    /*
    ID
    -----------
    1(1 個資料列受到影響)
    */
      

  2.   


    up
    < 可以解决这个问题。