描述:我有表A,里面有个字段varchar id,表B,里面有字段为varchar b_id,
id与b_id相对应,我要实现的功能是,通过查表A的id字段来判断表A中有多少条等于b_id的信息,即select count(*) from A,B where A.id=B.b_id,
问题是我有几百个不同的表A的id需要运行上面这个语句,表A中记录上百万,每个都这么查效率太差了吧,求优化的查询。

解决方案 »

  1.   

    select A.id,isnull(count(B.b_id),0) from A left join B on A.id=B.b_id group by A.id
      

  2.   

    select count(*) from
    (
    select id from a1
    union all
    select id from a2
    union all
    select id from a3
    .....
    ) t,b
    where t.id = b.b_id
      

  3.   

    错了,是几百个不同的表B的b_id需要运行select count(*) from A,B where A.id=B.b_id,
      

  4.   

    select count(*) from a,
    (
    select b_id from b1
    union all
    select b_id from b2
    union all
    select b_id from b3
    .....
    ) t
    where a.id = t.b_id
      

  5.   

    子陌 的语句 A改成B B改成A 就行了
      

  6.   

    select B.b_id, isnull(A.num, 0) as num from B
    left join(
    select id, count(*) as num from A group by id) as A on B.b_id=A.id