to:xyxfly(汇编初学者 What lies before me?) 
  是的,算法我知道,我是想用存储过程实现,就这不会了

解决方案 »

  1.   

    呵呵,其实只是用sql来描述而已,差不多的,自己有没有写过试试?
      

  2.   

    1、用户表: 
    字段: 名字、部门ID、地区 
    2、部门表 
    字段: 部门ID、父部门ID、地区 
    3、用户任务表 
    字段: 任务编号、用户名称 
    4部门任务表 
    字段:任务编号、部门ID、地区 问题: 
    当我向部门任务表添加任务时,同时向属于该部门的用户的用户任务表添加相应的任务 
    请问如何编写SQL语句 
    大家帮帮忙 
    太谢谢了create table bb(部门ID int,任务编号 int)declare @a intset @a=100
    select 部门ID,任务编号 into #qq from 部门任务表
    select @a
    insert into bb(部门ID,任务编号) select distinct 部门ID,任务编号 from 部门任务表while @a>0
    begin
    set @a=(select count(a.部门ID) from 部门表  a inner join bb  b on b.部门ID=a.父部门ID)
    delete bb
    insert into bb(部门ID) select distinct a.部门ID from 部门表  a inner join bb  b on b.部门ID=a.父部门ID
    insert into #qq(部门ID,任务编号) select distinct a.部门ID,任务编号 from 部门表  a inner join bb  b on b.部门ID=a.父部门ID
    endinsert into 用户任务表 (任务编号,用户名称) select q.任务编号,a.名字 from 用户表 a inner join #qq q on
    q.部门ID=a.部门ID drop table bb
    drop table #qq看看这个对你有帮助吗?
      

  3.   

    xyxfly(汇编初学者 What lies before me?)   帮帮我吧
      

  4.   

    DECLARE @tb TABLE([username] varchar(10), [L] varchar(10), [r] varchar(10), [registerDate] datetime)
    INSERT INTO @tb
        SELECT  'a', 'b', 'c', '2006-11-1'
        UNION ALL SELECT 'b', 'd', 'e', '2006-11-3'
        UNION ALL SELECT 'c', 'f', 'g', '2006-11-30'
        UNION ALL SELECT 'd', 'h', null, '2006-11-11'
        UNION ALL SELECT 'e', null, null, '2006-11-11'
        UNION ALL SELECT 'f', null, null, '2006-11-11'
        UNION ALL SELECT 'g', null, null, '2006-11-11'
        UNION ALL SELECT 'h', null, null, '2006-11-11'--SELECT * FROM @tb
    DECLARE @T TABLE(username VARCHAR(10), registerDate DATETIME)
    DECLARE @U VARCHAR(10)
    DECLARE @Seartype tinyint
    --设置查询用户
    SET @U = 'a'
    DECLARE @C INT
    --设置查询类型:0查左子树,1查右子树,2查所有
    SET @Seartype = 0
    IF @Seartype = 0
        --查询左子树
        INSERT INTO @T
            SELECT L, registerDate FROM @tb WHERE username = @U
    ELSE IF @Seartype = 1
        --查询右子树
        INSERT INTO @T
            SELECT R, registerDate FROM @tb WHERE username = @U
    ELSE IF @Seartype = 2
        ---查询所有
        INSERT INTO @T
            SELECT L, registerDate FROM @tb WHERE username = @U
            UNION ALL SELECT R, registerDate FROM @tb WHERE username = @USET @C = @@ROWCOUNT
    WHILE @C <> 0
    BEGIN
    INSERT INTO @T 
        SELECT A.L, A.registerDate FROM
        (SELECT L, registerDate FROM @tb
            WHERE username IN(SELECT username FROM @T) AND L IS NOT NULL)A
        LEFT JOIN @T B
        ON A.L = B.username AND A.registerDate = B.registerDate
        WHERE B.username IS NULL
    SET @C = @@ROWCOUNT
    INSERT INTO @T
        SELECT A.R, A.registerDate FROM
        (SELECT R, registerDate FROM @tb
            WHERE username IN(SELECT username FROM @T) AND R IS NOT NULL)A
        LEFT JOIN @T B
        ON A.R = B.username AND A.registerDate = B.registerDate
        WHERE B.username IS NULL
    SET @C = @C + @@ROWCOUNT
    END--得到的是所有的结果,有日期可以直接对结果进行查询
    SELECT * FROM @T
      

  5.   

    第一个你看看有没有问题
    create table Users

       UserName varchar(20),  --用户名
       L  varchar(20),        --记录它的左子结点的username
       R  varchar(20),        --记录它的右子结点的username
       RegisterDate datetime      --用户的注册日期,这个也很重要,用于结算时用
    )
    insert Users
    select 'a',  'b', 'c',   '2006-11-1'
     union all
    select 'b',  'd', 'e',   '2006-11-3' union all
    select 'c',  'f', 'g',   '2006-11-30' union all
    select 'd',  null, null, '2006-11-11' union all
    select 'e',  null, null, '2006-11-11' union all
    select 'f',  null, null, '2006-11-11' union all
    select 'g',  null, null, '2006-11-11'
    select count(1) as Cou from users a join users b on a.UserName=b.L
    where a.username not in(
    select a.l from users a join users b on a.username=b.r and a.l is not null)drop table Users
      

  6.   

    hhhdyj(萤火虫) ,xyxfly(汇编初学者 What lies before me?) ,谢谢,麻烦你们在帮我想想,
    我在网吧,现在没发测试,
    明天回公司试一下
      

  7.   

    --参考:
    create table Users

       UserName varchar(20),  --用户名
       L  varchar(20),        --记录它的左子结点的username
       R  varchar(20),        --记录它的右子结点的username
       RegisterDate datetime      --用户的注册日期,这个也很重要,用于结算时用
    )
    insert into  Users select 
       'a',          'b',     'c',      '2006-11-01' union all select
       'b',          'd',     'e',      '2006-11-03' union all select
       'c',          'f',     'g',      '2006-11-30' union all select
       'd',          null, null,    '2006-11-11' union all select
       'e',          null, null,    '2006-11-11' union all select
       'f',          null, null,    '2006-11-11' union all select
       'g',          null, null,    '2006-11-11'
    create function f_getOrigin(@UName varchar(20))
    returns varchar(1000)
    as
    begin
    declare @oStr varchar(1000)
    set @oStr='['+@UName+']'
    while exists(select 1 from Users where L=@UName or R=@UName)
    select @oStr=@oStr+'['+UserName+']',@UName=UserName from Users where L=@UName or R=@UName
    return @oStr
    end
    --1
    select count(1) from Users 
    where charindex('['+(select L from Users where UserName='a')+']',dbo.f_getOrigin(UserName))>0
    --2
    select count(1) from Users 
    where charindex('['+(select L from Users where UserName='a')+']',dbo.f_getOrigin(UserName))>0
    and convert(varchar(7),RegisterDate,120)=convert(varchar(7),(select RegisterDate from Users where UserName='a'),120)
    --3
    select count(1) from Users 
    where charindex('['+(select R from Users where UserName='a')+']',dbo.f_getOrigin(UserName))>0
    and convert(varchar(7),RegisterDate,120)=convert(varchar(7),(select RegisterDate from Users where UserName='a'),120)
    --4
    select count(1) from Users 
    where charindex('['+'a'+']',dbo.f_getOrigin(UserName))>0
    and convert(varchar(7),RegisterDate,120)=convert(varchar(7),(select RegisterDate from Users where UserName='a'),120)
    --5
    select count(1) from Users 
    where charindex('['+'a'+']',dbo.f_getOrigin(UserName))>0
    --本期的概念可能理解不对,你自己弄吧
      

  8.   

    --隨便寫一個? 所謂A的左子樹是不是指(B,D,E)
    --我的理解是,A中間一劈,左邊的都算左子樹
    --建個function
    go
    create function f_lr(@L varchar(20))
    returns  @t table(L varchar(20),[level] int)
    as
    begin  declare @level int
      set @level =1
      insert into @t select @L,@level
    while @@rowcount>0
    begin
       set @level=@level+1 
      insert into @t 
            select a.L,@level  from T a,@t b  where a.username=b.L  and  b.[level]=@level-1
             union all
            select a.R,@level from T a,@t b where a.username=b.L and b.[level]=@level-1 and @level>=3end
    delete @t where L is null
    return
    endgo--借用ls的測試數據:
    create table T

       UserName varchar(20),  --用户名
       L  varchar(20),        --记录它的左子结点的username
       R  varchar(20),        --记录它的右子结点的username
       RegisterDate datetime      --用户的注册日期,这个也很重要,用于结算时用
    )
    insert into T
    select 'a',  'b', 'c',   '2006-11-1'
     union all
    select 'b',  'd', 'e',   '2006-11-3' union all
    select 'c',  'f', 'g',   '2006-11-30' union all
    select 'd',  null, null, '2006-11-11' union all
    select 'e',  null, null, '2006-11-11' union all
    select 'f',  null, null, '2006-11-11' union all
    select 'g',  null, null, '2006-11-11'--結點'a'的左子樹:select a.* from T a,f_lr('a') b
    where a.username=b.l/* 結果:
    UserName             L       R          RegisterDate 
    --------------------------------------------------------------------------
    a                    b       c          2006-11-01 00:00:00.000
    b                    d       e          2006-11-03 00:00:00.000
    d                    NULL    NULL       2006-11-11 00:00:00.000
    e                    NULL    NULL       2006-11-11 00:00:00.000
    */
      

  9.   

    你的所有功能都可以用sql server的函数实现,不过,效率估计好不了,这是sql server函数本身限制的
      

  10.   

    可以付现金给 playwarcraft(时间就像乳沟,挤挤还是有的)  了分给我
      

  11.   

    --如果理解沒錯的話,就好辦了:select count(*)-1  as [結點a的左子樹的結點總數]  from T a,f_lr('a') b
    where a.username=b.l/*
    結點a的左子樹的結點總數 
    ------------ 
    3
    */--假如本期是'2006-11-10' 到 '2006-11-15'
    select count(*) as [本期結點a的左子樹新增結點]  from  T a,f_lr('a') b
    where a.username=b.l
    and RegisterDate between '2006-11-10' and '2006-11-15'/*
    本期結點a的左子樹新增結點 
    ------------- 
    2
    */
      

  12.   

    hhhdyj(萤火虫)的方法我试过了,基本上能解决1~5个问题,我现在联系他。
    xyxfly(汇编初学者 What lies before me?) 的方法对第一个节点可以,下面的就不行了,
    playwarcraft(时间就像乳沟,挤挤还是有的)和 zsforever(虎虎)    的方法,我不大看的懂,
    另外谢谢根铁的人这个帖子里还有一个问题,
    新加用户时,应该不是太复杂,我也贴出来说一下吧。
    比如说现在树的结构已经是上面这样了。A又要介绍M进来,而且A要把M放在A的左子树里,这时,我们有个约定,这时只能放在A的左子树的最左下的那个位置。只能放在D的左子结点这个位置了。
    同理,如果A要把M放在A的右子树里,这时只能放在A的右子树的最右下的那个位置。
    现在如何写一存储过程,
      GetNodePos
      输入:string aUserName
            char   cLR          //左子树还是右子树  = L  or R
                                //L表示左,R表示右
      输出:string sUserName  //返回aUserName最左下的那个node。
      功能:返回aUserName最左下的那个node。这个node得到后,就可以把M放在这个node的左下了。比如我想找a的最左下的节点,应该找到d,
    这个过程应该怎么写
    不要忘了留你的联系方式,
      

  13.   

    --如果套用偶給的那個function,基本都可以解決樓主的問題.
    --假如是A介紹進來,放左子樹,理論上要放在d結點
    go
    create function f_lr(@L varchar(20))
    returns  @t table(L varchar(20),[level] int)
    as
    begin  declare @level int
      set @level =1
      insert into @t select @L,@level
    while @@rowcount>0
    begin
       set @level=@level+1 
      insert into @t 
            select a.L,@level  from T a,@t b  where a.username=b.L  and  b.[level]=@level-1
       /*只是加最底層的左結點,所以左子樹的相對(右結點可以不要)*/
    end
    delete @t where L is null
    return
    endgo--借用ls的測試數據:
    create table T

       UserName varchar(20),  --用户名
       L  varchar(20),        --记录它的左子结点的username
       R  varchar(20),        --记录它的右子结点的username
       RegisterDate datetime      --用户的注册日期,这个也很重要,用于结算时用
    )
    insert into T
    select 'a',  'b', 'c',   '2006-11-1'
     union all
    select 'b',  'd', 'e',   '2006-11-3' union all
    select 'c',  'f', 'g',   '2006-11-30' union all
    select 'd',  null, null, '2006-11-11' union all
    select 'e',  null, null, '2006-11-11' union all
    select 'f',  null, null, '2006-11-11' union all
    select 'g',  null, null, '2006-11-11'
    --取level最大的那個左結點:
    select top 1 L  from f_lr('a') A order by [level] desc/*結果: 結點d
    L                    
    -------------------- 
    d
    */drop table t
    drop function f_lr
      

  14.   

    用以前的数据
    DECLARE @username varchar(3)
    SET @username = 'a'
    WHILE exists(SELECT TOP 1 1 FROM @tb WHERE username = @username AND l IS NOT NULL)
        SELECT @username = l FROM @tb WHERE username = @usernameSELECT @username
      

  15.   

    DECLARE @tb TABLE([username] varchar(10), [L] varchar(10), [r] varchar(10), [registerDate] datetime)
    INSERT INTO @tb
    SELECT 'a', 'b', 'c', '2006-11-1'
    UNION ALL SELECT 'b', 'd', 'e', '2006-11-3'
    UNION ALL SELECT 'c', 'f', 'g', '2006-11-30'
    UNION ALL SELECT 'd', 'h', null, '2006-11-11'
    UNION ALL SELECT 'e', null, null, '2006-11-11'
    UNION ALL SELECT 'f', null, null, '2006-11-11'
    UNION ALL SELECT 'g', null, null, '2006-11-11'
    UNION ALL SELECT 'h', null, null, '2006-11-11'
    (1)
    declare @count int
    declare @i int
    declare @tname varchar(30)
    set @count=0
    set @i=0
    set @tname='a'
    while @tname is not null
    begin
    select @tname=L from @tb where username=@tname
    set @i=@i+1
    end
    print @i(2)
    declare @count int
    declare @i int
    declare @tname varchar(10)
    declare @ddate datetime
    set @count=0
    set @i=0
    set @tname='a'
    while @tname is not null
    begin
    select @tname=L,@ddate=registerDate from @tb where username=@tname
    if(@ddate='2006-11-3')
    set @i=@i+1
    end
    print @i(3)
    declare @count int
    declare @i int
    declare @tname varchar(10)
    declare @ddate datetime
    set @count=0
    set @i=0
    set @tname='a'
    while @tname is not null
    begin
    select @tname=r,@ddate=registerDate from @tb where username=@tname
    if(@ddate='2006-11-3')
    set @i=@i+1
    end
    print @i(4)
    (2)+(3)(5)
    (1)+(1)r
      

  16.   

    1:如何求出A这个用户他左子树一共有多少结点?
      2:本期内,A的左子树一共新加了多少结点?
      3:本期内,A的右子树一共新加了多少结点?
      4:本期内,A的共新加了多少结点?
      5:A一共有多少子孙结点?
    打算在数据库一级写“存储过程”和“函数”,然后页面里调用
    -----------------------------------------------------------楼主这样的方案效率太低了,建议不能这样做.1.你每注册一个,它的位置和,有多少个结点,你都要遍历一次.这样你还想放在页面里调用,那就更慢了.2.就算遍历也要找一种算法吧.按照楼主的意思,可采有像中序一样的算法.
    3.还有应该选择双向链式结构,楼主的结构只是单向的.
      

  17.   

    主要是给hhhdyj(萤火虫)了。他人很好,给我了不少的帮助,只是他这段时间太忙了。