假如有如下数据:
1,有栏目a,b,c等等,它们下面分别又有子栏目,子栏目又有子栏目,如此类推的树型数。
2,同级栏目可以排列顺序。
3,在数据库的字段为
  kindid: 栏目编号
  parentid: 所属栏目的编号 (如果为0则自己是父结点。为0的父结点可以有很多)
  haschild: 是否有子栏目(0为没有,1为有)
  vkindid: 在所属栏目下,栏目在同级栏目中的顺序位置。
  kindlevel: 栏目所在的层次(级别)如有一个数据
   kindid  parentid  haschild  vkindid  kindlevel
   
   189       0          1        2         1
    12       189        1        2         2
    421      11         0        23        2
                     .
                     .
                     .
                     .
                     .如第一条记录表示:栏目编号为189,没有父结点,有子栏目(子结点),
                  在parentid=0的同级栏目(此时为1级栏目)中,它的位置是2,
 
第二条: 栏目编号为12,所属栏目编号为189,有子结点,在所属栏目编号为189下的
        所有栏目中,它的位置是2.它是二级栏目
问题::
   按图书目录结构式的对数据排序.
   排序后的把kindid,顺序保存在数组中.
    
   如例:
   kindid  parentid  haschild  vkindid  kindlevel
   
   189       0          1        2         1
    12       189        1        2         2
    421      11         0        23        2
    99       12         0        1         3
    11       12         1        2         3
   157        0         0        1         1
   32         0         0        以上数据排出的顺序应该是:
   kindid             顺序:
    157................1
    189................2
       12..............3
          99...........4
          11...........5
             421.......6
    32.................7根据以上所述和例子.用数据表中的某些字段.对表进行按以上规律和规则
排列.

解决方案 »

  1.   

    select * from 表名 start with parentid=0 connect by kindid=parentid;
    意思:根据根结点生成所有的子树。
    在按照层次分组进行排序
    整个SQL为:
      select * from (select * from 表名 start with parentid=0 connect by kindid=parentid)
        where 1=1  group by kindevel order by vkindid
      

  2.   

    to spinfollj:
       SQL不能运行啊
      

  3.   

    select * from 表名 start with parentid=0 connect by kindid=parentid 是oracle 数据库的用法,sql2000里不支持
      

  4.   

    树的遍历问题,要实现起来很简单。这里提供一个方法。
    1、先建一棵树,比如就用TreeView控件吧。
    2、将数据导入到树,比如Treeview中去。
    3、遍历该树。如果是TreeView则:
    for I:=0 to Treeview1.Items.Count-1 do
      数组[i]:=Treeview1.Items[I].text;
    即可。
    其实就是深度遍历树的问题。
      

  5.   

    给段加到TreeView的代码你改一下吧,大同小异。Type
      TTypeNode = record
        TreeNode:TTreeNode;
        ID:Integer;
      end;
    var
      I:Integer;
      J:Integer;
      PNode:TTreeNode;
      A,B:array of TTypeNode;
      quExecute:TADOQuery;
    begin
      quExecute:=TADOQuery.Create(nil);
      quExecute.ConnectionString:=ConnectionString;
      Treeview.Items.Clear;
      PNode:=Treeview.Items.Add(nil,'所有物料');
      quExecute.Close;
      quExecute.SQL.Clear;
      quExecute.SQL.Text:=Format('Select * from [%s] where Type_Level=1',['Type_Info']);
      quExecute.Open;
      SetLength(A,0);
      I:=0;
      while not quExecute.Eof do
      begin
        SetLength(A,High(A)+2);
        A[I].TreeNode:=TreeView.Items.AddChild(PNode,quExecute.FieldByName('Type_name').AsString);
        A[I].ID:=quExecute.FieldByName('Type_ID').AsInteger;
        I:=I+1;
        quExecute.Next;
      end;
      while not (High(A)=-1) do
      begin
        SetLength(B,0);
        J:=0;
        for I:=0 to High(A) do
        begin
          quExecute.Close;
          quExecute.SQL.Text:=Format('Select * from [%s] where Father_Id=%d',
            ['Type_Info',A[I].ID]);
          quExecute.Open;
          while not quExecute.Eof do
          begin
            SetLength(B,High(B)+2);
            B[J].TreeNode:=TreeView.Items.AddChild(A[I].TreeNode,quExecute.FindField('Type_Name').AsString);
            B[J].ID:=quExecute.FieldByName('Type_ID').AsInteger;
            J:=J+1;
    //        B[J].ImageIndex:=21;
            quExecute.Next;
          end;
        end;
        A:=B;
      end;
      quExecute.Close;
    end;