我有一个10万数据的表,名称为 移动手机号码 表中的数据是手机号码,另一个表中同样是移动手机号码表,表名是 移动禁止号码,但是这个表中有手机号码也有号段,例如:1326121  13361214,139512164,13951425 等长度不等的号段,要求 移动手机号码 表中手机号码的前几位和移动禁止号码表中的号段和手机号码相同的屏蔽掉。
我写的语句为:select  [移动手机号码].Col001
from [移动手机号码] inner join 移动禁止号码 
on substring([移动手机号码].Col001,1,len(移动禁止号码.Col001))<> 移动禁止号码.Col001此语句有问题,进入了死运算,请教格位高手,怎么写?谢谢

解决方案 »

  1.   

    select
        a.Col001
    from 
        [移动手机号码] a
    inner join 
        [移动禁止号码] b
    on  
        a.Col001 not like b.Col001+'%'
      

  2.   

    在 [移动手机号码] 和 [移动禁止号码] 的 Col001 上创建索引。BTW:短信群发?
      

  3.   

    --测试数据表
    create table test(a varchar(11))
    insert into test select 12314008308
    union all select  12345678901
    union all select  12315400923
    union all select  12345678901
    union all select  12315400923
    union all select  13845678901
    union all select  13814000923
    union all select  12345678901
    union all select  12315400923
    union all select  12345678901
    union all select  12315400923
    create table test1(a varchar(11))
    insert into test1 select 1384
    insert into test1 select 123
    --将黑名单表中的号码段后面加上字符'a'后再插入黑名单表,这样处理是为了排序
    insert into test1 select a+'a' from test1
    --效果:黑名单号码将不可用的号码包围了起来.只要用游标处理一下就可以了.不用做恐怖的大表关联
    select * from
    (
    select 1 c,* from test 
    union all
    select 2 c,* from test1
    )a
    order by a
    --删除测试
    drop table test
    drop table test1
      

  4.   

    select *
    from test as a 
    where  exists(select * from test1 as b where a.a like b.a+'%')用雪地撒野的例子做的
    最后选出13814000923
      

  5.   

    能够自动处理的完整高效方案:
    --测试数据表
    create table test(num varchar(11))--手机号码表
    insert into test select 12314008308
    union all select  12345678901
    union all select  12315400923
    union all select  12345678901
    union all select  12315400923
    union all select  13845678901
    union all select  13814000923
    union all select  12345678901
    union all select  12315400923
    union all select  12345678901
    union all select  12315400923
    create table test1(num varchar(11))--黑名单(号码或号码段)表
    insert into test1 select 1384
    insert into test1 select 123
    --将黑名单表中的号码段后面加上字符'a'后再插入黑名单表,这样处理是为了下面排序方便
    insert into test1 select num+'a' from test1
    --排序后效果:黑名单号码将不可用的号码包围了起来.将这个结果集按原序插入带有标志列的表#
    create table #(id int identity,tag int,num varchar(12))
    insert into # (tag,num)
    select * from(
    select 1 tag,num from test 
    union all
    select 2 tag,num from test1)a
    order by a.num
    --将表#中的黑名单项的信息转入带有标志列的表#1,以便自动获得奇偶性,便于下面的自动处理.
    create table #1(id int identity,tag2_id int)
    insert into #1
    select id from #
    where tag=2
    --拼凑动态sql语句,将#中的被包括在黑名单中的项(包括黑名单项)删除
    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+case when id%2=1 then (' delete from # where id between '+cast(tag2_id as varchar)+' and ') else cast(tag2_id as varchar) end 
    from #1
    exec(@sql)
    --此时表#中num字段存的都是符合要求的手机号.
    select num from #
    --删除测试
    drop table test
    drop table test1
    drop table #
    drop table #1
    --这么绕来绕去的处理,主要是为了效率,但我没有合适的大表做试验,希望楼主测试一下,给个说法~
      

  6.   

    dongrenyi()  作的还是不可以,运行不出结果
      

  7.   

    你的号码字段是什么类型?  如果是numeric的那需要转换
      

  8.   

    select *
    from test as a 
    where  exists(select * from test1 as b where a.a like cast(b.a as varchar)+'%')
      

  9.   

    数据量太大,上面的各位的运行结果都产生了偏差,要么进入死循环,要么结果不对,最后我写了这样的一条语句
    delete from  移动手机号码
    where col001 in (select mobile from 移动禁止号码)
    and left(col001,6) in (select mobile from 移动禁止号码)
    and left(col001,7) in (select mobile from 移动禁止号码)
    and left(col001,8) in (select mobile from 移动禁止号码)
    这样结果就出来了
      

  10.   

    不过我很佩服specialsoldier(雪地撒野)的知识能力,我给了他10分,但是你的方法,理论上可以,但是不怎么实用,我必须在5分钟内出结果。谢谢回帖子的朋友,感谢你们,每人5分
      

  11.   

    delete from  移动手机号码
    where col001 in (select mobile from 移动禁止号码)
    and left(col001,6) in (select mobile from 移动禁止号码)
    and left(col001,7) in (select mobile from 移动禁止号码)
    and left(col001,8) in (select mobile from 移动禁止号码)
    ------------------------
    你可以删除原始表? 而且你的黑名单号码段只有11位,6位,7位,8位四种?
    早说的话,如果只针对这么个特定的问题处理不考虑扩展,我们也许会有其他的变通方法.不过还是谢谢你的鼓励~哈哈