表A:a1,a2,a3,a4...
表B:b1,b2,b3,b4...select count(*) from A a, B b where a.a1 = '20090902' and a.a2 = b.b1;select count(*) from A where a1 = '20090902' and a2 in(select b1 from B);执行的时候查询出的值是不一样,请问一下这2个sql语句的区别,谢谢。

解决方案 »

  1.   

    如果1个a.a2可能对应多个b.b1,则两个值是不一样的。
    因为在连接结果中,a中的一条记录可能出现多次
      

  2.   

    你将第一句中的count(*)改成a.*
    第二句中的count(*)改成*
    对比一下结果
      

  3.   

    考虑连接可能产生的笛卡尔积,统计单表记录时最好不要用表连接
    楼主可以用下面代码来统计
    select count(1) from A where 
      exists(select 1 from b where b1=a.a2)
      and a1 = '20090902' ;