id    pid
    1     -1
    3     1
    4     3
    5     3  
     .......输入id为5后,获得它之前的所有id,4、3、1。这个怎么写?(pid表示它的前一个id)

解决方案 »

  1.   

    select id from tb where id < 5
      

  2.   

    加上 order by id asc
      

  3.   

    同问啊。见贴,
    http://community.csdn.net/Expert/topic/5220/5220860.xml?temp=.2868769
      

  4.   

    create function fn_getPar(@sonid int)
    returns @temptab table(id int,level int)
    as
    begin
     declare @level int
     set @level=1
     insert into @temptab(id,level) values(@sonId,@level)
     while @@rowcount>0
     begin
      SET @Level=@Level+1
      INSERT @temptab SELECT a.parentid,@Level
      FROM organise a,@temptab b
      WHERE a.Id=b.id
           AND b.Level=@Level-1
     end
       return
    end
    goselect id from dbo.fn_getPar(要查找的id) where id<>'要查找的id'
      

  5.   

    这个能在mysql在运行么?
    我把我的表替换进去 结果是
    Error Code : 1064
    You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '(@sonid int)
    returns @temptab table(id int,level int)
    as
    beg
    (0 ms taken)