建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > MYSQL学习 > mysql怎么批量替换sql语句

mysql怎么批量替换sql语句

时间:2015-04-28 15:17:20  来源:免费模板网 作者:风雪 阅读次数 tags字符串处理

在mysql字符替换我们常用用update更新语句与replace替换函数进行操作,我们可以利用where来限制是替换指定内容还是替换所有内容,下面一起来看看相关教程.

replace替换:mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace().

replace:replace(str1, str2, 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替换表的字段里面内容,代码如下:

  1. mysql> select id,type from items limit 10; 
  2. +--------+--------+ 
  3. | id     | type   | 
  4. +--------+--------+ 
  5. |   0001 | 780000 | 
  6. |   0002 | 780000 | 
  7. |   0003 | 780000 | 
  8. |   0004 | 780000 | 
  9. |   0005 | 780000 | 
  10. | 150419 | 780000 | 
  11. | 150420 | 780000 | 
  12. | 150421 | 780000 | 
  13. | 150422 | 780000 | 
  14. | 150423 | 780000 | 
  15. +--------+--------+ 

把type字段中的“78”改成“79” 用replace函数,sql如下:

  1. mysql> update items set type=replace(type,'79','78'); 
  2.  
  3. Query OK, 17536 rows affected (0.72 sec) 
  4. Rows matched: 17536  Changed: 17536  Warnings: 0 

再查询,代码如下:

  1. mysql> select id,type from items limit 10; 
  2. +--------+--------+ 
  3. | id     | type   | www.phpfensi.com 
  4. +--------+--------+ 
  5. |   0001 | 790000 | 
  6. |   0002 | 790000 | 
  7. |   0003 | 790000 | 
  8. |   0004 | 790000 | 
  9. |   0005 | 790000 | 
  10. | 150419 | 790000 | 
  11. | 150420 | 790000 | 
  12. | 150421 | 790000 | 
  13. | 150422 | 790000 | 
  14. | 150423 | 790000 | 
  15. +--------+--------+ 
  16. 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:

  1. +----+------------------------------------------+  
  2. | id | name                                     |  
  3. +----+------------------------------------------+  
  4. |  1 | sdfsf<contact>beijing</contact>sldjfsld  |  
  5. |  2 | sdfsf<contact>shanghai</contact>sldjfsld |  
  6. |  3 | sdfsf<contact>jn</contact>sldjfsld       |  
  7. |  4 | sdfsf<contact>qd</contact>sldjfsld       |  
  8. +----+------------------------------------------+  

遇到的要求是:将该表中<contact>到</contact>的内容删除,众所周知,replace函数是不支持正则表达式的,所以只能采用其他的方法处理.

于是,我是使用了下面的sql语句:

update mt2 set name = replace(name, substring(name, locate('<contact>', name),locate('</contact>', name)-locate('<contact>'+10, name)),'');  

问题解决了,结果如下:

  1. +----+-------------------+  
  2. | id | name              |  
  3. +----+-------------------+  
  4. |  1 | sdfsfactsldjfsld |  
  5. |  2 | sdfsfactsldjfsld |  
  6. |  3 | sdfsfactsldjfsld |  
  7. |  4 | sdfsfactsldjfsld |  
  8. +----+-------------------+
  9.  

本文地址:https://www.freemoban.com/mysql/2015/0428/1588.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!