有三个表a,b和ca:
a1,a2,a3
1 001 233
2 002 sd3
3 001
...b:
b1,b2,b3
1  22 23
1  2s 11
1  23 2d
1  23 23
2  ss ss
2  32 wa
2  we 2s
3  ed ss2
3  s1 23
3  3d sa
...c:
c1, c2,c3
001 1
001 1
001 1
001 1
...在DBGrid或cxGrid或其他Grid控件里显示表c的数据!就是当表a1=1时,表b中b1=1的记录数有几条,那就在表c的c2=1中增加相应的记录数(c1=a2).

解决方案 »

  1.   

    "就是当表a1=1时,表b中b1=1的记录数有几条,那就在表c的c2=1中增加相应的记录数(c1=a2).
    "这里你描述的不清楚。用sql select嵌套就可以了啊。
      

  2.   

    你自己再稍微改改,可以通过组件将结果显示出来:var 
      i,j,counts1,counts2 : integer;
    with Tquery.Create(application)  do
      try
        close;
        SQL.Clear;
        SQL.Add( 'select a1,a2,count(*) as counts1 from a  ');
        open;
        
        i := 0;
        while not eof do
        begin
          if i < counts1 then
          begin
            a1 := FieldByName('a1').AsString;
            a2 := FieldByName('a2').AsString;
      with Tquery.Create(application)  do
       try
           close;
             SQL.Clear;
           SQL.Add( 'select b1,count(b1) as counts2 from a,b  where a1 = b1 group by b1  ');
              open;
              i := i + 1;
      j := 0;
          while not eof do
           begin
               if i < counts2 then
               begin
                  with Tquery.Create(application)  do
             try
            close;
              SQL.Clear;
            SQL.Add( 'insert into b values(a2,a1)  ');
               open;
      j := j + 1;
    next;
          Finally
         Close;
         Free;
             end;
        end;
        next;
      end;
      Finally
           Close;
           Free;
       end;
          end;
          next;
        end;
      Finally
        Close;
        Free;
      end;