PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 数据库 > MySQL教程 > 正文

    mysql 替换字符串sql语句

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:replace替换mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便。 mysql 替换...
    replace替换
    mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便。 mysql 替换函数replace()
    replace
    replace(str1, str2, str3): 在字串 str1 中,? str2 出??r,?⑵湟 str3 替代。
    例子
    UPDATE `table_name` SET `field_name` = replace (`field_name`,'from_str','to_str') WHERE `field_name` LIKE '%from_str%'
    说明: 
    table_name —— 表的名字 
    field_name —— 字段名 
    from_str —— 需要替换的字符串 
    to_str —— 替换成的字符串
    例如:
    mysql替换表的字段里面内容,如例子:
    mysql> select id,type from items limit 10;
    +--------+--------+
    | id     | type   |
    +--------+--------+
    |   0001 | 780000 |
    |   0002 | 780000 |
    |   0003 | 780000 |
    |   0004 | 780000 |
    |   0005 | 780000 |
    | 150419 | 780000 |
    | 150420 | 780000 |
    | 150421 | 780000 |
    | 150422 | 780000 |
    | 150423 | 780000 |
    +--------+--------+
    把type字段中的“78”改成“79” 用replace函数
     
    sql如下:
    mysql> update items set type=replace(type,'79','78');
    Query OK, 17536 rows affected (0.72 sec)
    Rows matched: 17536  Changed: 17536  Warnings: 0
    再查询:
    mysql> select id,type from items limit 10;
    +--------+--------+
    | id     | type   | www.111cn.net
    +--------+--------+
    |   0001 | 790000 |
    |   0002 | 790000 |
    |   0003 | 790000 |
    |   0004 | 790000 |
    |   0005 | 790000 |
    | 150419 | 790000 |
    | 150420 | 790000 |
    | 150421 | 790000 |
    | 150422 | 790000 |
    | 150423 | 790000 |
    +--------+--------+
    10 rows in set (0.00 sec)
    由查询结果到,数据已经更新成功
    正则替换
    locate:
    LOCATE(substr,str) 
    POSITION(substr IN str) 
    返回子串 substr 在字符串 str 中第一次出现的位置。如果子串 substr 在 str 中不存在,返回值为 0:
    substring
    SUBSTR(str,pos,len): 由<str>中的第<pos>位置开始,选出接下去的<len>个字元。
    首先描述一下,我遇到的问题:
    以下是数据库中的一个表mt2: 
    +----+------------------------------------------+ 
    | id | name                                     | 
    +----+------------------------------------------+ 
    |  1 | sdfsf<contact>beijing</contact>sldjfsld  | 
    |  2 | sdfsf<contact>shanghai</contact>sldjfsld | 
    |  3 | sdfsf<contact>jn</contact>sldjfsld       | 
    |  4 | sdfsf<contact>qd</contact>sldjfsld       | 
    +----+------------------------------------------+ 
    遇到的要求是:将该表中<contact>到</contact>的内容删除。 
    众所周知,replace函数是不支持正则表达式的,所以只能采用其他的方法处理。 
    于是,我是使用了下面的sql语句: 
    Sql代码 
    update mt2 set name = replace(name, substring(name, locate('<contact>', name),locate('</contact>', name)-locate('<contact>'+10, name)),''); 
    问题解决了。 
    结果: 
    +----+-------------------+ 
    | id | name              | 
    +----+-------------------+ 
    |  1 | sdfsfactsldjfsld | 
    |  2 | sdfsfactsldjfsld | 
    |  3 | sdfsfactsldjfsld | 
    |  4 | sdfsfactsldjfsld | 
    +----+-------------------+
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-35-535-1.html
    相关热词搜索: 替换 字符串 replace