类别                 编号
001                1
001                2
001                3
001                6
001                7
002                4
002                5
现在记录定位在编号7,下一条应该是4,这个sql语句如何写

解决方案 »

  1.   

    SELECT ID=IDENTITY(INT,1,1),* INTO # FROM tbSELECT TOP 1 
        类别,
        编号
    FROM #
    WHERE ID>(SELECT ID
              FROM #
              WHERE 编号=7)
    ORDER BY ID 
      

  2.   

    select (select top 1 编号 from T where (类别=a.类别 and 编号>a.编号) or 类别>a.类别 order by 类别,编号) from T a where a.编号=7
      

  3.   

    现在记录定位在编号7,下一条应该是4,这个sql语句如何写
    ------------------------------------------------
            你是怎么定位在编号7的? 
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([类别] varchar(3),[编号] int)
    insert [tb]
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5select top 1 a.* 
    from tb a, tb b
    where cast(a.类别 as int)=cast(b.类别 as int)+1
    and b.编号=7
    order by a.编号--测试结果:
    /*
    类别   编号
    ---- -----------
    002  4
    */
      

  5.   

    create table #tb([Type] varchar(3),Num int)
    insert #tb
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5select * from #tb order by [Type],Num 
    不是很懂Lz的意思
      

  6.   

    我是在.net做的页面上传的一个参数id
      

  7.   

    具体是这样的,做的一个页面显示信息的详细内容,这个页面上有上一条和下一条的按钮,需要按照类别和信息id顺序显示,如果当前是id=3,那么下一个就是6,而不是4,当前是7的时候,下一条才是4,因为首先要按照类别排序,不知道我说清楚没有,各位老大
      

  8.   

    --测试数据
    declare @t table(类别 varchar(3), 编号  int)
    insert @t
    select '001',1 union
    select '001',2 union
    select '001',3 union
    select '001',6 union
    select '001',7 union
    select '002',4 union
    select '002',5 
    declare @编号 int
    set @编号=7--查询
    select top 1 * from @t where 类别>(select  类别 from @t where 编号=@编号)
    or (类别=(select  类别 from @t where 编号=@编号) and 编号>@编号)
    /*
    类别   编号          
    ---- ----------- 
    002  4
    */
      

  9.   

    to:longlyhawk,我试了一下,不对,比如:
    id      classid
    1 001
    2 001
    3 001
    4 002
    5 002
    6 002
    7 001
    8 001
    9 002
    如果当前是6的话,下一条应该是9,但是你的代码查出来的是7
      

  10.   

    但是如果是这样的,就不对了
    id      classid
    1       001001
    2       001001
    3       001001
    4       002001
    5       002001
    6       002001
    7       001001
    8       001001
    9       002001
    10      001002
    11      001002
    因为这里classid(类别号)是存在子项的,比如001001001,002001001等等
      

  11.   

    真不了解是程序和SQL哪个实现方便。
      

  12.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([类别] varchar(3),[编号] int)
    insert [tb]
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5
    IF OBJECT_ID('TEMPDB..#')IS NOT NULL DROP TABLE #
    GO
    SELECT *,ID=IDENTITY(INT,1,1)INTO # FROM TB ORDER BY [类别] ,[编号]
    DECLARE @编号 INT
    SET @编号=3
    SELECT 类别 ,  编号 FROM # WHERE  ID IN(SELECT ID+1 FROM # WHERE [编号]=@编号 )
    SET @编号=7
    SELECT 类别,   编号 FROM # WHERE  ID IN(SELECT ID+1 FROM # WHERE [编号]=@编号 )
    /*类别   编号          
    ---- ----------- 
    001  6类别   编号          
    ---- ----------- 
    002  4
    */
      

  13.   

    把where 后的classid都换成right(classid,3)
      

  14.   


    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb](lei varchar(3),id int)
    insert [tb]
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5
    --select * from tb
    declare @a int,@b varchar(10),@i int
    set @a=7   --输入的参数值
    select @b=lei from tb where id=@a
    set @i=(select count(*) from tb where lei=@b and id>@a)
    if @i <>0
    select @a=(select top 1 id from tb where lei=@b and id>@a)
    else
    begin
    select @b=(select top 1 lei from tb where lei>@b)
    select @a=(select min(id) from tb where lei=@b)
    end
    select @a
    drop table tb
    /*
    -----------
    4(1 行受影响)
    */
      

  15.   

    把你部分数据和SQL语句贴出来看看
      

  16.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb](lei varchar(3),id int)
    insert [tb]
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5declare @id_tmp int,@lei_tmp varchar(3)set @id_tmp=2
    set @lei_tmp='001'
    if (select max(id) from tb where lei=@lei_tmp group by lei)>@id_tmp
    select top 1 * from tb where id>@id_tmp order by lei,id
    else 
    begin
    select top 1 * from tb where lei>@lei_tmp order by lei,id
    end
      

  17.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb](lei varchar(3),id int)
    insert [tb]
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5declare @id_tmp int,@lei_tmp varchar(3)set @id_tmp=2
    set @lei_tmp='001'
    if (select max(id) from tb where lei=@lei_tmp group by lei)=@id_tmp
    select top 1 * from tb where lei>@lei_tmp order by lei,id
    else 
    begin
    select top 1 * from tb where id>@id_tmp and lei =@lei_tmp order by lei,id
    end/*
    测试数据    结果
    ---------------
    001 2       001 3
    001 7       002 4
    002 5     null
    002 4     002 5
    */
      

  18.   


     declare @T table([Type] varchar(3),ID int)
     insert into @T
     select '001','1' union all
     select '002','2' union all
     select '003','3' union all
     select '001','6' union all
     select '001','7' union all
     select '001','4' union all
     select '001','5'   select eid=identity(int,1,1),* into #T from @T
     
      select top 1 * from #T
      where eid>

      select top 1 eid from #T where id=2
     ) 
     drop table #T
      

  19.   

    if object_id('[tb]') is not null drop table [tb]
    go
    /*
    1      001001 
    2      001001 
    3      001001 
    4      002001 
    5      002001 
    6      002001 
    7      001001 
    8      001001 
    9      002001 
    10      001002 
    11      001002 
    */create table [tb](id int,lei varchar(3))
    insert [tb]
    select 1,'001' union all
    select 2,'001'union all
    select 3,'001'union all
    select 4,'002'union all
    select 6,'002'union all
    select 5,'002'union all
    select 7,'001'declare @id_tmp int,@lei_tmp varchar(3)set @id_tmp=1
    set @lei_tmp='001'
    --select top 10000 * from tb  order by lei,id
    if (select max(id) from tb where lei=@lei_tmp group by lei)=@id_tmp
    select top 1 * from (select top 10000 * from tb  order by lei,id)a where lei>@lei_tmp
    else 
    begin
    select top 1 * from (select top 10000 * from tb order by lei,id)a where id>@id_tmp and lei=@lei_tmp
    end
      

  20.   

    SELECT ID=IDENTITY(INT,1,1),* INTO # FROM tbSELECT TOP 1 
        类别,
        编号
    FROM #
    WHERE ID>(SELECT ID
              FROM #
              WHERE 编号=7)
    ORDER BY ID 
      

  21.   


    declare @table table (m_type varchar(3),m_no int)
    insert into @table
    select '001',1 union all
    select '001',2 union all
    select '001',3 union all
    select '001',6 union all
    select '001',7 union all
    select '002',4 union all
    select '002',5select row_number()over(order by m_type, m_no) as rownum, * from @table
    /*
    rownum               m_type m_no
    -------------------- ------ -----------
    1                    001    1
    2                    001    2
    3                    001    3
    4                    001    6
    5                    001    7
    6                    002    4
    7                    002    5
    */