需求根据表tab2的quhao,统计出表tab1中相同地区(区号)的数量,并倒序排序.
例子
表tab1:id    tele
       1   010-11111111
       2   0532-1515151
       3   0532-1510000
       4   0532-2333333
       5   010-16666666表二tab2:id  quhao
         1   010
         2   0532要求得到结果
quhao count
0532  3
010   2我试着用下面的两种方法,但是都不行.请高手告知更好的方法.谢谢
select tab2.quhao,count(tab1.id) as count
from tab2,tab1
where tab1.tele like tab2.quhao%或者
select tab2.quhao,count(tab1.id) as count
from tab2
left join tab1 
on tab1.tele like tab2.quhao%

解决方案 »

  1.   

    create table temp111 (tele varchar2(100));
    insert into temp111 values('010-11111111');
    insert into temp111 values('0532-1515151');
    insert into temp111 values('0532-1510000');
    insert into temp111 values('0532-2333333');
    insert into temp111 values('010-16666666');create table temp222 (quhao varchar2(100));
    insert into temp222 values('010');
    insert into temp222 values('0532');
    select t2.quhao,count(t2.quhao) m_count from temp111 t1 inner join temp222 t2 on substr(t1.tele,1,length(t2.quhao))=t2.quhao
    group by t2.quhao