第一个表 telphone字段如下:
mobile
15026721234
15935981234
第二个表 haoduanshengfen mobile上海 1502672
山西 1593598现在,我要根据号码段 来查找这个号码所属省份
mobile
15026721234
15935981234
第二个表 haoduanshengfen mobile上海 1502672
山西 1593598现在,我要根据号码段 来查找这个号码所属省份
select telphone.mobile,haoduan.shengfen from telphone inner join haoduan on left(telphone.mobile,7)=haoduan.mobile;
这是数据库里面的
库名:phone
表名:bc_haoduan
字段: mobile city
1313456 北京
1353456 上海
要导入数据库(库名phone)的表是 telphone
里面有如下字段:
mobile dtime uid city
现在要求:
将telphone里mobile与bc_haoduan里的mobile进行查询
telphone.mobile里的数据为11位
bc_haoduan.mobile里的只有7位
执行一条语句
如果telphone.mobile里的数据在bc_haoduan.mobile范内,
则自动把bc_hoduan.city插入telphone.city里面。
set a.city=b.city
set a.city=b.city
INSTR(str,substr)
返回字符串 str 中子字符串的第一个出现位置。出现则>0
update telphone a inner join haoduan b on instr(a.mobile,b.mobile )>0
set a.city=b.shengfen
INSTR(str,substr) Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed. mysql> SELECT INSTR('foobarbar', 'bar');
-> 4
mysql> SELECT INSTR('xbar', 'foobar');
-> 0不过用INSTR,不能使用索引
bc_haoduan.mobile里的只有7位
如果使用这个就更新不了啊。
update telphone a inner join bc_haoduan b on instr(a.mobile,b.mobile)>0
set a.city=b.city
update telphone a inner join bc_haoduan b on a.mobile regexp b.mobile
set a.city=b.city
这样可以用索引
update telphone a inner join bc_haoduan b on (substring(a.mobile,1,7) regexp b.mobile)>0
set a.city=b.city
这样写吗?
SELECT INSTR('15026721234','1502672')看看结果
纠正上述,REGEXP不能用到索引
还有,我的MYSQL在跑了。
update telphone a inner join bc_haoduan b
on a.mobile like concat(b.mobile,'%')
set a.city=b.city
OR
update telphone a inner join bc_haoduan b
on a.mobile like b.mobile set a.city=b.city
哪个市 modile=187297% 建立数据字典可以解决。
update telphone a inner join bc_haoduan b
on a.mobile like concat(b.mobile,'%')
set a.city=b.city
set a.city=b.city或者
update telphone a inner join bc_haoduan b
on a.mobile like concat(b.mobile,'%')
set a.city=b.city显然速度会很慢,应该使用update telphone a inner join bc_haoduan b
on left(a.mobile,7)=b.mobile
set a.city=b.city
update telphone a inner join bc_haoduan b
on a.mobile like b.mobile set a.city=b.city
on a.mobile like concat(b.mobile,'%')
set a.city=b.city
如果在mobile上建立索引,是会用到索引的
语句需要索引,这样会加快查询速度。导入其它机器,看你是如何导入的了,如果是用mysqldump 导出,则目标机器上会创建索引的,不需要重建。
show index from telphone ;
show index from bc_haoduan ;
explain update telphone a inner join bc_haoduan b
on left(a.mobile,7)=b.mobile
set a.city=b.city;或者其它你的语句的EXPLAIN结果,这样大家才能进行分析。
方法肯定是有,关键是自己想不想,动不动脑筋。
可以上传到 http://www.access911.net/csdn