需求统计表中相同地区(区号)的数量,并倒序排序.  
例子  
表tab:id        tele  
           1      010-11111111  
           2      0532-1515151  
           3      0532-1510000  
           4      0532-2333333  
           5      010-16666666  
 
要求得到结果  
quhao  count  
0532    3  
010      2  
 
我试着这样写,可是总是出错.  
select  tele(0,4)  count(id)  as  count  from  tab  group  by  tele(0,4)    
请高手指点,我该怎样才能更好的得到我要的结果

解决方案 »

  1.   

    select substr(tele,0,4)  as tel,count(*) as count from tab group by tel desc;lz试试
      

  2.   

    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
      

  3.   

    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
    order by t2.quhao desc;
      

  4.   

    多谢,原来取部分应该这样写,substr(tele,0,4) 呵呵,谢谢
    这样就OK了,count 
    select  substr(tele,0,4) as tel,count(*) as count 
    from tab group by substr(tele,0,4) order by count desc
      

  5.   

    substr(tele,0,4)??? 
    1 010-11111111
    2 0532-1515151 
    取出的东西不一样啊,对于0532是但是010就不是了
      

  6.   

    如果判断-的位置进行截取可以这样
    select t2.quhao,count(t2.quhao) m_count from temp111 t1 inner join temp222 t2 on substr(tele,0,instr(tele,'-',1)-1)=t2.quhao
    group by t2.quhao
    order by t2.quhao desc;
      

  7.   

    OK instr(tele,'-',1)这个函数不错,谢谢你的关注, 失踪的月亮
      

  8.   

    select 1=1,1='1', '1.0'=1, '1'='1.0', '01' = ' 1', '01' = '  1 ', 1='01 ', '1 '=1, '1.'=1, 1.00=1.0, '1.00'='1.0';What's the result in MYSQL? If using Oracle, add "from dual" at the end.