table1
顾客 序号  
A    001   
A    002   
A    003   
B    001  
C    001 
  
table2
序号  是什么东西
001      thing1
002      thing2
003      thing3查询返回得到顾客        以购物品        
A     thing1,thing2,thing3   
用AdoDataSet谢谢
 

解决方案 »

  1.   

    好像只能用Delphi循环控制,生成变量,拼接SQL语句
      

  2.   

    howto?我已经想了好久了,还是调不好请教了
      

  3.   

    select 顾客,things from tbl1,tbl2 where tbl1.顾客=A and
    tbl2.序号 in (select 序号 from tbl1 where tbl1.顾客=A) 
    and tbl1.序号=tbl2.序号
      

  4.   

    写的简略一些:
    AdoQry1.Sql.Text:='select distinct 顾客 from Table1';
    AdoQry1.Open;while not AdoQry1.Eof do
    begin
      StrCustomer:=Trim(AdoQry1.FieldByName('顾客').AsString);
      AdoQry2.Sql.Text:='select distinct b.是什么东西 '+
                        'from Table1 a,Table2 b '+
                        'where a.顾客='''+StrCustomer+''' '+   
                        'and a.序号=b.序号';
      AdoQry2.Open;
      StrGoods:='';
      while not AdoQry2.Eof do
      begin
        StrGoods:=StrGoods+Trim(AdoQry2.FieldByName('是什么东西').AsString)+',';
        AdoQry2.Next;
      end;
      StrGoods:=Copy(StrTemp,1,Length(StrTemp)-1);  //把StrCustomer和StrGoods插入一个临时表#Table中。不要用AdoQry1!  AdoQry1.Next;
    end;select * from Table
      

  5.   

    一条查询SQL实现的确有点难,除非使用临时表:
    select distinct a.Customer,
    (case when (b.GoodsID = '001' or b.GoodsID = '002' or b.GoodsID = '003') then goodsname else null end) as info
    from T_Purchase a, T_GoodsInfo b where a.GoodsID=b.GoodsID
      

  6.   

    以解决了
    十分感谢heixiu1980(heixiu1980)