數據如下﹕
create tabel aa(classID varchar(10),className varchar(50) parent varchar(10))
insert into aa
select 
A , 輔料             , NULL  union all select 
A-01 , 日常輔料             , A  union all select 
A-01-001 , 布碎                 , A-01  union all select 
A-01-002 , 手套                , A-01  union all select 
A-01-003 , 掃把                 , A-01  union all select 
A-01-004 , 書寫品               , A-01  union all select 
A-01-005 , 衛生品               , A-01  union all select 
A-01-006 , 用具                 , A-01  union all select 
A-02 , 生產輔料             , A  union all select 
A-02-002 , 樹脂板               , A-02  union all select 
A-02-003 , 生粉                 , A-02  union all select 
A-02-004 , 燃油                 , A-02  union all select 
A-02-005 , 膠水                 , A-02  union all select 
A-02-006 , 包裝帶              , A-02  union all select 
A-02-007 , 釘線                 , A-02  union all select 
A-02-008 , 啤具                 , A-02  union all select 
A-02-009 , 化工品               , A-02  union all select 
A-02-011 , 其它                 , A-02  union all select 
A-02-012 , 膠紙                 , A-02  union all select 
A-02-013 , 海綿                 , A-02  union all select 
A-02-014 , 四色水墨             , A-02  union all select 
A-02-015 , 雜色水墨             , A-02 select * from aadrop table aa
.........................................................................

解决方案 »

  1.   

    怎么沒有人回答呢﹐我自己寫了一個這樣的﹐不知道怎么可以優化一下;procedure TgoodsClassFrm0.createTree;
    var
      root: TTreeNode;
    begin //1
      root := classTree.Items.AddFirst(nil, '所有物料分類');
      createLeaf(root);
      classtree.Items.GetFirstNode.Expand(false);
    end; //1procedure TgoodsClassFrm0.createLeaf(parent: TTreeNode);
    var
      node: TTreeNode;
      cond: string;
    begin //1
      with tadoquery.Create(nil) do
      begin //2
        if Mstr(parent.Text, '(', ')') = '' then
          cond := ' where parentClass is null '
        else
          cond := ' where parentClass =''' + Mstr(parent.Text, '(', ')') + '''';
        Connection := purchdm.conn;
        close;
        sql.Clear;
        sql.add('select * from goodsClass ' + cond +
          ' order by classID');
        open;
        while not eof do
        begin
          node := classTree.Items.AddChild(parent, '(' + trim(
            fieldbyname('classID').AsString) + ') ' + trim(
            fieldbyname('className').AsString));
          if recordExists('select 1 from store..goodsClass where parentClass =''' +
            fieldbyname('classID').AsString + '''') then
            createLeaf(node);
          next;
        end;
        close;
        free;
      end; //2
    end; //1
      

  2.   

    你的sql语句和Delphi代码都好乱。对于这种情况,比较常规的做法就是用递归。对于你的算法,可以改进的地方是不用每一层递归都创建一个tadoquery,而可以考虑用一个tadoquery把所有信息都取出来,然后在每一层递归里使用过滤的方式来取得需要的数据,这样效率会高一些
      

  3.   

    回复人: hunterht(核桃II——俺就是核桃的马甲) :用一個query的filter怎么寫呀﹐我怎么也寫不對﹐怎么保存前一filter的結果呀