现有两张表如下:
表A:
a1
2014
2301
2032
2003
2604
2065
2006
2087
2808
2009
2010
2012
2045
2109
2245表B
b1       b2
2000     2100
2100     2250
2300     2500
2500     2999现在我的要求是将表B中的一行的b1和b2作为一个数字段,查询出在表A中的数字对应在表B中各个数字段的个数,这个该怎么做呢?敬请高手指教,谢谢!

解决方案 »

  1.   

    select count(1) from a
     where exists(select 1 from b where b1=a.a1 or b2=a.a1)
      

  2.   


    想要的结果是这样的:
    b1      b2       count(a1)
    2000    2100        10
    2100    2250        2
    2300    2500        1
    2500    2999        2
      

  3.   

    select B.B1, B.B2, count(1) from B, A
    where A.A1 >= B.B1 and A.A1 < B.B2
    group by B.B1,B.B2
      

  4.   


    select b.b1,b.b2,count(1) from a,b
      where a.a1>=b.b1 and a.a1<b.b2
      group by b.b1,b.b2
      order by b.b1
      

  5.   

    考虑到有可能某个范围的值在a表中个数会为零,修改下
    select b.b1,b.b2,count(a.a1) from a,b
      where a.a1(+)>=b.b1 and a.a1(+)<b.b2
      group by b.b1,b.b2
      order by b.b1
      

  6.   

    差不多的用法,写法差别点select B.b1,B.b2,count(1)
    from B
    left join A
    on B.b1 <= A.a1
    and B.b2 > A.a1
    group by B.b1,B.b2;
      

  7.   

    楼上的不把count(1)改成count(a.a1)会出错的
      

  8.   

    有道理,如果用1的话,实际值为null时,统计值也会为1。应该改下。select B.b1,B.b2,count(a.a1)
    from B
    left join A
    on B.b1 <= A.a1
    and B.b2 > A.a1
    group by B.b1,B.b2;