有2张表a,b,判断a中的字段1在b中的字段2中是否存在,怎么实现,谢谢各位达人帮忙!

解决方案 »

  1.   

    select count(*) as acount from a,b where a.f1=b.f2
      

  2.   

    Tadotable1对应a表的数据,Tadotable2对应b表的数据。
    for i:=0 to ADOTable1.recordcount-1 do
      begin
        for j:=0 to ADOTable2.recordcount-1 do
        begin
          if ADOTable1.FieldByName('字段1').value=ADOTable2.FieldByName('字段2').value then
            ShowMessage('the same result');
          else
            ADOTable2.next;
        end;
        ADOTable1.next;
      end;
    这种方法将a表和b表中所有的数据都遍历了一遍,效率比较低。
    建议用SQL语句:
      select * from a,b where a.field1=b.field2
    如果返回值>0,则有相同的值;=0无相同值
      

  3.   

    支持jinjazz(近身剪(N-P攻略))
      

  4.   

    select count(*) as count from a where 字段1 in (select distinct 字段2 from 表2)
    当返回的count大于0,则表明表2的字段2包含有表1的字段1的内容
      

  5.   

    select a.* from a where exist (select b.field2 from b where b.field2=a.field1)
      

  6.   

    select a.* from a where exist (select b.field2 from b where b.field2=a.field1)
    ——效率高select 1 from a where exist (select b.field2 from b where b.field2=a.field1)
    ——效率最高
      

  7.   

    更正:上述语句,应该是Exists,不是Exist