字段 phone:13706143678
1380539929015633137726
15633137751
15633137795
15633137870
15633137891
15633137897
15633137899
1563313792514723450975
1492345097515181362125
15181362128
15181362129
15181362131
15181362135
1518136213913923450975号码前六位相同(SUBSTRING (phone,0, 7))视为连续,要提取 不连续的号码 结果:13706143678
13805399290
14723450975
14923450975
13923450975

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([phone] bigint)
    insert [tb]
    select 13706143678 union all
    select 13805399290 union all
    select 15633137726 union all
    select 15633137751 union all
    select 15633137795 union all
    select 15633137870 union all
    select 15633137891 union all
    select 15633137897 union all
    select 15633137899 union all
    select 15633137925 union all
    select 14723450975 union all
    select 14923450975 union all
    select 15181362125 union all
    select 15181362128 union all
    select 15181362129 union all
    select 15181362131 union all
    select 15181362135 union all
    select 15181362139 union all
    select 13923450975
    goselect * from tb t
    where (select count(1) from tb where left(phone,7)=left(t.phone,7))=1/**
    phone
    --------------------
    13706143678
    13805399290
    14723450975
    14923450975
    13923450975(5 行受影响)
    **/
      

  2.   

    --or
    select * from tb t
    where not exists(select 1 from tb where left(phone,7)=left(t.phone,7) and phone!=t.phone)
      

  3.   

    如果 要剔除 条件:连续的号码的个数>n (如 3)的呢
      

  4.   

    select * from tb t
    where (select count(1) from tb where left(phone,7)=left(t.phone,7))=1
    把这里的=1改成你要的条件即可
      

  5.   

    try
    select a.* from tb a
    join (select left(phone,7) as p from tb group by left(phone,7) having count(1)>3) b
    on left(a.phone,7)=b.p