我有一个10万数据的表,名称为 移动手机号码 表中的数据是手机号码,另一个表中同样是移动手机号码表,表名是 移动禁止号码,但是这个表中有手机号码也有号段,例如:1326121 13361214,139512164,13951425 等长度不等的号段,要求 移动手机号码 表中手机号码的前几位和移动禁止号码表中的号段和手机号码相同的屏蔽掉。
我写的语句为:select [移动手机号码].Col001
from [移动手机号码] inner join 移动禁止号码
on substring([移动手机号码].Col001,1,len(移动禁止号码.Col001))<> 移动禁止号码.Col001此语句有问题,进入了死运算,请教格位高手,怎么写?谢谢
我写的语句为:select [移动手机号码].Col001
from [移动手机号码] inner join 移动禁止号码
on substring([移动手机号码].Col001,1,len(移动禁止号码.Col001))<> 移动禁止号码.Col001此语句有问题,进入了死运算,请教格位高手,怎么写?谢谢
a.Col001
from
[移动手机号码] a
inner join
[移动禁止号码] b
on
a.Col001 not like b.Col001+'%'
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
from test as a
where exists(select * from test1 as b where a.a like b.a+'%')用雪地撒野的例子做的
最后选出13814000923
--测试数据表
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
--这么绕来绕去的处理,主要是为了效率,但我没有合适的大表做试验,希望楼主测试一下,给个说法~
from test as a
where exists(select * from test1 as b where a.a like cast(b.a as varchar)+'%')
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 移动禁止号码)
这样结果就出来了
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位四种?
早说的话,如果只针对这么个特定的问题处理不考虑扩展,我们也许会有其他的变通方法.不过还是谢谢你的鼓励~哈哈