用递归,不断地进行SELECT查询,当然条件也不断变化
你的问题说得不够清楚,我也只能说到这里。数据结构学得如何?

解决方案 »

  1.   

    可以呀,没问题的,以任何符合ID,Caption,Parent的数据表记录都可以生成树。
      

  2.   

    简单
    //打开一个工程
    Procedure TDBMain.OpenPrj(PrjID:Integer);
    Var I,N:Integer;
        ANode: TTreeNode;
        NewNode:Boolean;
    Begin
         TreeView1.Enabled:=False;
         TreeView1.Items.Clear;     //工程名称
         PubQuery.Close;
         PubQuery.SQL.Clear;
         PubQuery.SQL.Add('Select Name,NameC From DBMaster');
         PubQuery.SQL.Add('Where ID='+IntToStr(PrjID));
         PubQuery.Open;     If PubQuery.FieldByName('Name').IsNull Then Exit;     ANode:=TreeView1.Items.Add(Nil,PubQuery.FieldByName('NameC').AsString+'['+PubQuery.FieldByName('Name').AsString+']');
         ANode.StateIndex:=PrjID;     Try
         NewNode:=True;
         While NewNode Do
         Begin
              NewNode:=False;
              N:=TreeView1.Items.Count;
              For I:=0 To N-1 Do
              If TreeView1.Items[I].Count=0 Then
              Begin
                   PubQuery.Close;
                   PubQuery.SQL.Clear;
                   PubQuery.SQL.Add('Select * From DBMaster');
                   PubQuery.SQL.Add('Where UpID='+IntToStr(TreeView1.Items[I].StateIndex));
                   PubQuery.SQL.Add('Order By Ord');
                   PubQuery.Open;               While Not PubQuery.Eof Do
                   Begin
                        ANode:=TreeView1.Items.AddChild(TreeView1.Items[I],PubQuery.FieldByName('NameC').AsString+'['+PubQuery.FieldByName('Name').AsString+']');
                        ANode.StateIndex:=PubQuery.FieldByName('ID').AsInteger;
                        If Not NewNode Then
                           NewNode:=True;                    PubQuery.Next;
                   End;
              End;
         End;
         Finally
                TreeView1.Enabled:=True;
         End;     TreeView1.FullExpand;
         CurrentProjectID:=PrjID;
         CurrentItemID:=-1;
    End;
    效率不高
      

  3.   

    这样的记录:
    F1          F2     F3          ...    Fn     OldID
    -----------------------
    体育与健康  NULL   NULL         ..    NULL   100                  
    体育与健康  健康   NULL         ..    NULL   756          
    体育与健康  健康   运动参与     ..    NULL  160                
    ...         ...          ...         ...   ...  ..      ...    转换成下面的形式:
    NodeName        NodeID       ParentID         Path 
    体育与健康       5            0               0>
    健康            14            5               0>5>
    运动参与        78           14               0>5>14>也就是抛弃原来的结构冗余。重建一颗“学科知识点”的树!
    我现在思路是有了。
    但是我不知道如何递归的查询每条记录的第一个为NULL的
    字段之前的字段的内容(树的子节点)
    =========================
    新表(NodeTable)的结构:
    NodeId int
    NodeName nvarchar(200)
    ParentID int
    path  varchar(200)
    -----------------
    原表(T1...T19表示19 个学科)的结构
    F1~F5  或者F1~F6 或者F1~F7   都是nvarchar(100) (每个学科的知识层次不一样) 
      

  4.   

    to: AP() 老兄
    我的最终问题是如何从数据库表(未转换)中读出记录,
    然后生成NodeId,ParentID ,Path
    最终得到新的表结构!!!!我要在程序中生成NodeId,并不是像老大那样的“从表中读PrjID”
      

  5.   

    这是我在大富翁上的问题的部分回答(伪代码)
    来自:Adnil, 时间:2002-7-26 16:48:00, ID:1224913 
    转换的话也不困难啊,不用递归,稍微笨一点的方法select * from oldtable
    create cursor
      while not cursor.eof do
      getrecord
      for i = 1 to n //轮循字段
        select id = NodeID from newtable where Nodename = currecord("f"n)  //看Newtable中有没有记录
        if id = null then //没有
          insert into newtable(NodeName) values(currecord("f"n))
        else if i > 1
          select id = NodeID from newtable where Nodename = currecord("f"(n-1)) //前一个字段的ID
          update newtable set ParentID = id
        end if
      end for
      end while
    close cursor您对此有何看法??