我把这样的语句 select * from aaaa a,bbbb b where a.h=b.h坼成下面的语句是否更有效率。下面的语句行的通吗?bbbb的表内容很少。不超过100条。
var
i:integer;
f:string;
begin 
      adoquery2.close;
      adoquery2.sql.text := 'select h from bbbb'; 
      adoquery2.Open; 
    for i:=0 to adoquery2.recordcount-1 do
    begin
     f:=adoquery2.FieldValues['a'].asstring;
    with adoquery1 do 
      begin 
      close; 
      sql.text := 'select * from aaaa where h='+f; 
      Open; 
      end;
     adoquery2.next; 
    end; 
end; 还有其他方法吗?

解决方案 »

  1.   

    select * from aaaa where h in (select  h from bbbb)
      

  2.   

    select * from aaaa a,bbbb b where a.h=b.h
    这句比in效率高吧?
      

  3.   

     select * from aaaa a,bbbb b where a.h=b.h
    够简洁了吧?
      

  4.   

    看着别扭,如果真的要像你下边那么写,最好这样(纯属个人意见)var 
      i: integer;
      f: string;
    begin
      adoquery2.close;
      adoquery2.sql.text := 'select h from bbbb';
      adoquery2.Open;
      adoquery2.first;
      while not adoquery2.Eof do
      begin
        f:=adoquery2.FieldValues['a'].asstring;
        with adoquery1 do
        begin
         close;
         SQL.Clear;
         sql.text := 'select * from aaaa where h='+f;
         Open;
        end;
        adoquery2.next;
      end;
    end;
    如果h字段在数据库里为字符串类型,则需要将 sql.text := 'select * from aaaa where h='+f;改为
    sql.text := 'select * from aaaa where h = ''' + f + '''';
      

  5.   


    select * from aaaa a,bbbb b where a.h=b.h 這個好點
    下面的就是在FETCH,累死人了。
      

  6.   

    循环效率很低,多表联合有很多查询方式
    1.select * from 表1,表2....
    2.select * from 表1 where 表1.id in (select id from 表2)//这种效率低
    3.select * from 表1 inner join 表2 on 表1.id=表2.id//这种目前来说,为首选
      

  7.   

    两点否决第二种方案
    1、如果bbbb数据很多,但实际关联后结果其实很少的话,你干了无用功
    2、白白在程序多访问了数据库很多次,这个非常影响效率如果连连接查询的效率都觉得有问题的话,就要反思是不是数据库设计有问题了。
      

  8.   

    循环语句会报错
    应该select * from aaaa a,bbbb b where a.h=b.h或select * from aaaa c inner join bbbb d on c.h=d2.h