两个表,一对多,如何把“多”那个表的某列的字符串加起来?例如:
表a
id
1
2表b
id,aid,x
1,1,abc
2,1,def
3,2,gh结果
aid,y
1,abcdef
2,gh

解决方案 »

  1.   

    table1.close;
    table2.close;
    table1.mastersource := datasource1;  // 假设datasource1.dataset = table1
    table1.masterfields := 'aid';
    table2.open;
    table1.open;table1.first;
    while not table1.eof do
    begin
      table2.first; 
      str := '';
      while not table2.eof do
      begin
        str :=  str + Table2.FieldByName('x').asstring;
        table2.next;
      end;
      // str 为你所需要的结果,在此处进行处理,比如写入数据库等
      table1.next;
    end;table1.close;
    table2.close;