| 
 | 
 
 
本文实例讲述了dedecms使用SQL命令批量替换指定字符串的方法。分享给大家供大家参考。具体分析如下: 
几年前做的一个QQ站现在把资料整过来了,但是发现有些litpic缩略图中的路么地址不对了,以前我都是使用phpmyadmin替换,今天我发现可以直接使用SQL命令运行器来解决,下面来看我一个替换litpic中指定路径地址. 
查询指定 archives 表: 
复制代码代码如下:select * from 你的表前缀_archives limit 1 
这个表是主表了,我们查询一条出来看看,结果如下. 
运行SQL:select * from 你的表前缀_archives where id =1466,共有1条记录,最大返回100条. 
记录:1   
id:1466   
typeid:110   
typeid2:0   
sortrank:1238913636   
flag:c,p   
ismake:1   
channel:1   
arcrank:0   
click:33119   
money:0   
title:可爱的动态nomoQQ表情图片   
shorttitle:   
color:   
writer:   
source:   
litpic:http://upload.jb51.net/all-1SH6.jpg   
pubdate:1238913636   
senddate:1238913636   
现在我们可以锁定到id =1466 记录: 
复制代码代码如下:select litpic from 你的表前缀_archives where id =1466; 
执行结果: 
运行SQL: 
复制代码代码如下:select litpic from 你的表前缀_archives where id =1466;共有1条记录,最大返回100条. 
记录:1 
litpic:http://upload.jb51.net/all-1SH6.jpg 
好了现在我们需要把litpic 字段中的http://upload.jb51.net 替换成 /uploads/ 这种,这些我们可以使用update replace来实现. 
例子: 
复制代码代码如下:update 你的表前缀_archives set  `litpic`=replace(litpic, 'http://upload.jb51.net', '/uploads')  WHERE  id=1466 
我们来执行看看结果,成功执行1个SQL语句,好现在我们来查询一下执行结果是不是我们想要的,再执行: 
复制代码代码如下:select litpic from 你的表前缀_archives where id =1466; 
确定执行提示: 
运行SQL: 
复制代码代码如下:select litpic from 你的表前缀_archives where id =1466;共有1条记录,最大返回100条. 
记录:1 
litpic:/uploads/all-1SH6.jpg 
看提示没有错误,我们在地址栏看看图片正确,查看是没有问题了,现在我们来批量替换. 
复制代码代码如下:select litpic from 你的表前缀_archives where litpic like '%http://upload.jb51.net%' 
这样我们查一下是为了防止后面替换不会替换没有http://upload.jb51.net了,也防止了sql出错导致数据库全部替换掉了. 
复制代码代码如下:update 你的表前缀_archives set  `litpic`=replace(litpic, 'http://upload.jb51.net', '/uploads')  where litpic like '%http://upload.jb51.net%' 
成功执行1个SQL语句,我们再查一下看. 
运行SQL: 
复制代码代码如下:select litpic from 你的表前缀_archives where litpic like '%http://upload.jb51.net%'无返回记录! 
希望本文所述对大家的dedecms建站有所帮助。 
 |   
 
 
 
 |