根据电话号码的区号确定费率?

gxd305 2005-09-18 03:22:14
我现在做一个数据库
已经有一个表 table1,table2
table1字段如下
电话号码,费率(现在需要填写的)
table2字段如下
电话号码区号,费率(已知)

现在根据table2中的电话号码区号 以及 费率,来填写table1中的费率

比如
table1中有数据
电话号码 费率
01086956565 ----
02956565652 ----
table2中有数据
电话号码区号 费率
010 0.23
029 0.21


即可填写table1为
电话号码 费率
01086956565 0.23
02956565652 0.21

我是这么实现的
首先将table2中的数据用for 循环 一条一条取出来,然后update

update table1 set 费率=feilv where 电话号码 like "电话号码区号*"

这里有几个问题
1。处理的数据量比较大,table1数据10万条左右,table2数据1000条左右(意味着执行update 1000次)
2。电话号码的长度不一样
3。区号长度页不一样,即使区号一样,电话号码长度也不一定相同
所以实现的速度很慢,有没有比较好的方法,使速度快一些

用SQL Server 2000和BCB做的











...全文
198 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxd305 2005-09-20
  • 打赏
  • 举报
回复
.....
gxd305 2005-09-19
  • 打赏
  • 举报
回复
首先谢谢大家的关注
我回去测试了一下,wgsasd311(自强不息)的方法不能解决问题

charindex 返回的是子串在一字符串中首次出现的位置 如果大于0的话和实际需要的
区号和电话号码的前面的几位匹配,就是两个概念了
所以并不能得到正确的匹配

我借用wgsasd311(自强不息)的方法改了一下
update a
set 费率 = b.费率
from table1 a, table2 b
where a.电话号码 like b.电话号码区号+'%'
where a.费率 is null
这样的话在我有的测试数据中可以得到正确的结果

不过还存在一个问题

比如电话区号 有2,232,2324这样的话 实际用"2%","232%",“2324%”
都可以和"23242132525"这个电话号码匹配
那么数据库会怎么样匹配呢
当然我希望的是和最后一个匹配
有没有更好的方法?



bugchen888 2005-09-19
  • 打赏
  • 举报
回复
update a
set 费率 = b.费率
from table1 a, table2 b
where b.电话号码区号 = left(a.电话号码,len(b.电话号码区号))
mtu207 2005-09-18
  • 打赏
  • 举报
回复
wgsasd311(自强不息)真是非常认真!佩服!!


楼主的问题解决了,别忘了给wgsasd311(自强不息)散分哦:)
天地客人 2005-09-18
  • 打赏
  • 举报
回复
楼主搞定了?帮UP了
gxd305 2005-09-18
  • 打赏
  • 举报
回复
太感谢了!!!
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
declare @qu int
set @qu=(select max(len(电话号码区号)) from table2)
update table1 set 费率=null--初始化
while @qu>0
begin
update a set 费率=b.费率
from table1 a left join table2 b
on charindex(b.电话号码区号,left(a.电话号码,@qu))>0 where a.费率 is not null
set @qu=@qu-1
end
go
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
---把上面的is not null 改为 is null .
declare @qu int
set @qu=(select max(len(电话号码区号)) from table2)
update table1 set 费率=null--初始化
while @qu>0
begin
update a set 费率=b.费率
from table1 a left join table2 b
on charindex(b.电话号码区号,left(a.电话号码,@qu))>0 where a.费率 is null
set @qu=@qu-1
end
go
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
--不好意思上面写错了,请试下面:
declare @qu int
set @qu=(select max(len(电话号码区号)) from table2)
update table1 set 费率=null--初始化
while @qu>0
begin
update a set 费率=b.费率
from table1 a left join table2 b
on charindex(b.电话号码区号,left(a.电话号码,@qu))>0 where a.费率 is not null
set @qu=@qu-1
end
go
gxd305 2005-09-18
  • 打赏
  • 举报
回复
先谢谢了!
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
declare @qu int
set @qu=(select max(len(电话号码区号)) from table2)
update a set 费率=b.费率
from table1 a left join table2 b
on charindex(b.电话号码区号+space(@qu-len(b.电话号码区号)),left(a.电话号码,4))>0
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
请问下你的区号最长多少们
gxd305 2005-09-18
  • 打赏
  • 举报
回复
首先谢谢二楼的关注
至于能不能实现,我待会儿测试一下

还有个问题
存在这样的区号 01 与 012而他们的费率 并不是一样的
上面的方式可以实现吗

顺便说一下由于是 国际长途电话号码,区号的位数有两位的 ,三位的,。。。还有5位的
wgsasd311 2005-09-18
  • 打赏
  • 举报
回复
update a set 费率=b.费率
from table1 a left join table2 b
on charindex(b.电话号码区号,left(a.电话号码,4))>0

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧