第一个表 telphone字段如下:
   mobile 
15026721234
15935981234
第二个表 haoduanshengfen        mobile上海                     1502672
山西                     1593598现在,我要根据号码段 来查找这个号码所属省份

解决方案 »

  1.   


    select telphone.mobile,haoduan.shengfen from telphone inner join haoduan on left(telphone.mobile,7)=haoduan.mobile;
      

  2.   

    详细描述你的业务需求。CSDN里藏龙卧虎,也许你能从中有所领悟。
      

  3.   

    看表吧
    这是数据库里面的
    库名: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里面。
      

  4.   

    update telphone a inner join bc_haoduan b on instr(a.mobile,b.mobile )>0
    set a.city=b.city
      

  5.   

    update telphone a inner join bc_haoduan b on instr(a.mobile,b.mobile )>0
    set a.city=b.city
      

  6.   

    instr函数的一个方法,
    INSTR(str,substr) 
    返回字符串 str 中子字符串的第一个出现位置。出现则>0
      

  7.   

    1:查询select haoduan.shengfen,telphone.mobile from haoduan,telphone where instr(telphone.mobile,haoduan.mobile) > 02:更新
    update telphone a inner join haoduan b on instr(a.mobile,b.mobile )>0
    set a.city=b.shengfen
      

  8.   

    看看INSTR的HELP
    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,不能使用索引
      

  9.   

    telphone.mobile里的数据为11位
    bc_haoduan.mobile里的只有7位
    如果使用这个就更新不了啊。
    update telphone a inner join bc_haoduan b on instr(a.mobile,b.mobile)>0
    set a.city=b.city
      

  10.   

    在telphone、bc_haoduan的mobile上建立索引
    update telphone a inner join bc_haoduan b on a.mobile regexp b.mobile
    set a.city=b.city
    这样可以用索引
      

  11.   


    update telphone a inner join bc_haoduan b on (substring(a.mobile,1,7) regexp b.mobile)>0
    set a.city=b.city
    这样写吗?
      

  12.   

    不会吧
    SELECT INSTR('15026721234','1502672')看看结果
    纠正上述,REGEXP不能用到索引
      

  13.   

    regexp 是什么?
    还有,我的MYSQL在跑了。
      

  14.   

    用LIKE使用索引,假设前7位
    update telphone a inner join bc_haoduan b
    on a.mobile like concat(b.mobile,'%')
    set a.city=b.city
      

  15.   

    正则表达式
    OR
    update telphone a inner join bc_haoduan b
    on a.mobile like b.mobile set a.city=b.city
      

  16.   

    尽力索引。通过条件利用_和%这两个来查询例如:187使移动where  mobile=187%  
     哪个市 modile=187297%  建立数据字典可以解决。
      

  17.   

    你测试一下不就知道了
    update telphone a inner join bc_haoduan b
    on a.mobile like concat(b.mobile,'%')
    set a.city=b.city
      

  18.   

    我是在我的电脑上跑的。CPU 1.6 内存2G
      

  19.   

    不知道MYSQL是不是死了,我靠。
      

  20.   

    你的SELECT语句是什么? 如果是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 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
      

  21.   

    你的索引情况是怎样的
    update telphone a inner join bc_haoduan b
    on a.mobile like b.mobile set a.city=b.city
      

  22.   

    update telphone a inner join bc_haoduan b
    on a.mobile like concat(b.mobile,'%')
    set a.city=b.city
    如果在mobile上建立索引,是会用到索引的
      

  23.   


    语句需要索引,这样会加快查询速度。导入其它机器,看你是如何导入的了,如果是用mysqldump 导出,则目标机器上会创建索引的,不需要重建。
      

  24.   

    没有具体定论。你可以贴出你的 
    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结果,这样大家才能进行分析。
      

  25.   

    那就分段贴。再不行就贴到文本文件中,然后上传。
    方法肯定是有,关键是自己想不想,动不动脑筋。
    可以上传到 http://www.access911.net/csdn
      

  26.   

    我靠,CSDN什么时候有上传的这个啊。我混了6年,今天才知道。
      

  27.   

    http://access911.net/csdn/FileDescription.asp?mdb=2010-11-4&id=1传好了。