one SQL statement is probably tough, consider to write a update trigger

解决方案 »

  1.   

    create function modifylasttime(@id int,@time datetime)
    begin  update table set lasttime=@time where id=@id
      
      if (select followid from table where id=@id)!=0 
      then
      modifylasttime(  
                      (select followid from table where id=@id),@time
                     )
    end
      

  2.   

    楼主问的是一条SQL语句 而不是函数或者存储过程 触发器
      

  3.   

    declare @id int
    set @id = 10
    while @id <> 0 
    begin
    select id from test where id = @id
    select @id = followId from test where id = @id
    end
      

  4.   

    一条Sql语句是不太可能的,因为这种操作肯定要用到递归,用存储过程、函数或触发器也是会用问题的,因为DBMS对嵌套的最大层数都是有限制的,SQL Server的最大嵌套层数为32,所以还是建议楼主在程序中处理
      

  5.   

    一般来说,你的FollowID习惯被命名为ParentID。另外你需要对这张表增加一个Path字段:I BbsName LastTime ParentID Path
    1 电脑网络 47:28.0 0        1
    2 影视下载 11:02.0 0        1
    3 游戏专区 47:35.1 0        1
    4 ASP.NET 17:23.0 1        1.4
    5 SQLServer 08:27.0 1        1.5
    6 初级 32:40.0 4        1.4.6
    7 中级 39:06.2 4        1.4.7
    8 高级 39:41.3 4        1.4.8
    9 将军 25:38.0 8        1.4.8.9
    10 司令 38:10.7 8        1.4.8.10如果你为一张表新增一个Path字段,这需要写一个递归程序来为每一条记录写Path值。但之后在维护时,无论是增加还是删除记录,维护Path都无须通过递归。
    接着,你要获得ID=10的Path,就是1.4.8.10,然后用Where InStr('1.4.8.10',Path)=1就可以分理出父链的记录。(InStr是VB函数,在Access数据库能够使用,如果是其他数据库,你需要查一下相应方法。)
      

  6.   

    这是论坛 BBSXP 的数据库,不知道它是怎么做的,没看出来,但它没有存贮过程和触发器.
      

  7.   

    BBSXP的数据库竟然没有表示Path的字段?不会吧……就算是n年之前的Dvbbs5都已经有啦!
      

  8.   

    ....确实是没有我现在要写一个模仿它的论坛,但用.net来写,代码离的,所以...它的源代码很难看地说....