table1
顾客 序号
A 001
A 002
A 003
B 001
C 001
table2
序号 是什么东西
001 thing1
002 thing2
003 thing3查询返回得到顾客 以购物品
A thing1,thing2,thing3
用AdoDataSet谢谢
顾客 序号
A 001
A 002
A 003
B 001
C 001
table2
序号 是什么东西
001 thing1
002 thing2
003 thing3查询返回得到顾客 以购物品
A thing1,thing2,thing3
用AdoDataSet谢谢
tbl2.序号 in (select 序号 from tbl1 where tbl1.顾客=A)
and tbl1.序号=tbl2.序号
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
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
十分感谢heixiu1980(heixiu1980)