select 
  b.area,
  count(1) as [用户数]
from tb_user a
left jion tb_area on left(a.mobile,7)=b.num
group by b.area
这样?

解决方案 »

  1.   

    try:
    select  left(a.mobile,7) 号码段 ,b.area 地区,count(a.id) 用户数 
    from tb_user a,tb_area b 
    where left(a.mobile,7)=b.num
    group by left(a.mobile,7),b.area
      

  2.   

    select b.area 地区,count(a.id) 用户数 
    from tb_user a,tb_area b 
    where left(a.mobile,7)=b.num
    group by b.area
      

  3.   

    declare @a table(id int identity,num varchar(20),area varchar(50))
    insert @a select '1300000','北京'
    union all select '1300012','天津'declare @b table(id int identity,mobile varchar(20))
    insert @b select '1300000qqee'
    union all select '1300000aadd'
    union all select '1300000aaad'
    union all select '1300000aaqd'
    union all select '1300012aa3d'
    union all select '1300012afdd'select *,用户数=(select count(*) from @b where left(mobile,7)=num)
    from @aid          num                  area                                               用户数
    ----------- -------------------- -------------------------------------------------- -----------
    1           1300000              北京                                                 4
    2           1300012              天津                                                 2(2 行受影响)
      

  4.   

    几百万级的记录,要分组,还要用left().没在mobile加个索引看看。
      

  5.   


    不好意思,刚才有点笔误---测试数据---
    if object_id('[tb_area]') is not null drop table [tb_area]
    go
    create table [tb_area]([id] int,[num] int,[area] varchar(4))
    insert [tb_area]
    select 1,1300000,'北京' union all
    select 2,1300012,'天津' union all
    select 120193,1595879,'浙江'
    if object_id('[tb_user]') is not null drop table [tb_user]
    go
    create table [tb_user]([id] int,[mobile] bigint)
    insert [tb_user]
    select 1,13800138000 union all
    select 2,13000009123 union all
    select 3,13000009124 union all
    select 4,13000009125 union all
    select 5,13000009126 union all
    select 6,13000009127 union all
    select 7,13000129120 union all
    select 8,13000129121 union all
    select 9,13000129122
     
    ---查询---
    select 
      b.area,
      count(1) as [用户数]
    from tb_user a
    left join tb_area b on left(a.mobile,7)=b.num
    group by b.area---结果---
    area 用户数         
    ---- ----------- 
    NULL 1
    北京   5
    天津   3(所影响的行数为 3 行)
      

  6.   


    select a.*  ,用户数=(select count(2) from tb_user  where left(mobile,7)=num  group by area,left(mobile,7))
    from tb_area a 
      

  7.   

    在num字段中有多条记录代表的都是一个地方,这样的话就不能用分组了吧? 如:
       num          area
     1302325 上海 1301284         上海 1301283         上海 1301282         上海 1301281         上海 1301280         上海
      

  8.   

    你说的是这个?
    select b.area 地区,count(a.id) 用户数 
    from tb_user a,tb_area b 
    where left(a.mobile,7)=b.num
    group by b.area这个可以执行,不过很慢
      

  9.   


    如果是这种情况的话,把表稍微该写,例如上海地区从那个段到哪个段,,然后
    select *--可以进行统计之类的
    from a,b
    where left(b.phone,7) between 起始段 and 终止段另外添加索引试试
      

  10.   

    试试用临时表会不会快一点,先按前7位手机号group by 再jion和按地区group by---测试数据---
    if object_id('[tb_area]') is not null drop table [tb_area]
    go
    create table [tb_area]([id] int,[num] int,[area] varchar(4))
    insert [tb_area]
    select 1,1300000,'北京' union all
    select 2,1300012,'天津' union all
    select 120193,1595879,'浙江'
    if object_id('[tb_user]') is not null drop table [tb_user]
    go
    create table [tb_user]([id] int,[mobile] bigint)
    insert [tb_user]
    select 1,13800138000 union all
    select 2,13000009123 union all
    select 3,13000009124 union all
    select 4,13000009125 union all
    select 5,13000009126 union all
    select 6,13000009127 union all
    select 7,13000129120 union all
    select 8,13000129121 union all
    select 9,13000129122
     
    ---插入临时表---
    select 
      left(mobile,7) as num,
      count(1) as [用户数]
    into #
    from tb_user
    group by left(mobile,7)---查询---
    select 
      b.area,
      sum(a.[用户数]) as [用户数]
    from # a
    left join tb_area b on a.num=b.num
    group by b.areadrop table #---结果---
    area 用户数         
    ---- ----------- 
    NULL 1
    北京   5
    天津   3(所影响的行数为 3 行)
      

  11.   

    这个方法我也想过了,还时有问题的,因为起始号码可能处于不同的号码段如1301280--1301289是上海,而1302320---1302329也是上海,等等其它号段也是上海,这样用between and 就很难实现了。现在在考虑在向用户表插入数据时查询地区表,把用户所在地区插入用户表中,当然在用户表中要新添加个字段。难道用sql语句就不能实现这个查询了吗?当然查询速度是能让人忍受的