用sql语句update批量替换WordPress表里某字段值
feilong.org 修订于2008-10-26 07:59:00 414 次浏览飞龙博客第一次用的dreamhost虚拟主机,第二次是hostdime的主机,第三次是lunarpages的虚拟主机。日志url静态链接方式从feilong.org/postname到feilong.org/index.php/postname,再回到feilong.org/postname 。之所以这样,是因为第二次的hostdime主机不支持url网址重写。这次飞龙不得不又来修改博客日志里链接。人工修改?那太可怕了,得找个批量替换修改网址的方法!
飞龙想直接用phpadmin的sql查询来解决网址替换。先备份数据库,并把用php myadmin数据库sql替换字段内容查看了分析分析,准备把飞龙博客里每个出现 feilong.org/index.php的地方小心翼翼换回feilong.org/。也就是批量修订wordpress日志内的index.php,记录成本日志,修订网址:http://feilong.org/phpmyadmin-sql-update-replace 飞龙第4次修订于2010011
一、进入cPanel的php myadmin,点击飞龙博客数据库名称,"导出"现有数据库到本地电脑。
二、点击php myadmin的"搜索",输入"/index.php"来查找到文字或数值,全选,执行。
三、sql查询的搜索结果:
"/index.php"的搜索结果 至少一个单词:
3 个匹配项 - 于表 feilong_comments 浏览
2 个匹配项 - 于表 feilong_links 浏览
6 个匹配项 - 于表 feilong_options 中 浏览
1 个匹配项 - 于表 feilong_postmeta 中 浏览
338 个匹配项 - 于表 feilong_posts 中 浏览
四、找到要修改的目标了,接着学习如何批量处理字段值。飞龙了解到可以使用SQL语句 update 批量处理字段值,包括在原字段内容批量添加或去掉字符。这次飞龙准备批量去掉字符"/index.php"。那么update命令该怎么使用呢?先看看update命令的语法。飞龙提示:命令里用半角字符,如果是具体字段值,左右必须加单引号。
update 某数据表名 set 此表某字段=REPLACE(此表某字段,'原来内容','新内容')
五、下面随飞龙一步步使用update命令
1、进入php myadmin,找到飞龙博客数据库feilongorgdb,点击phpmyadmin的"SQL" ,查询。
2、现在看看表feilong_comments,字段comment_author_url的内容为feilong.org/index.php 全部改为feilong.org。于是输入如下命令:
update feilong_comments set comment_author_url=REPLACE(comment_author_url,'http://feilong.org/index.php','http://feilong.org'
3、现在处理表feilong_comments。飞龙发现index.php出现在comment_author_url和comment_content这两个字段。于是输入如下命令:
update feilong_comments set comment_author_url=REPLACE(comment_author_url,'http://feilong.org/index.php','http://feilong.org')
update feilong_comments set comment_content=REPLACE(comment_content,'http://feilong.org/index.php','http://feilong.org')
4、处理表feilong_links。浏览字段数据飞龙发现index.php在link_url字段。于是输入如下命令:
update feilong_links set link_url=REPLACE(link_url,'http://feilong.org/index.php','http://feilong.org')
5、处理表feilong_options。浏览具体数据飞龙发现index.php在option_value字段,但是只有一个,不必批量处理,手工修改即可。
6、处理表feilong_postmeta。浏览分析后飞龙发现index.php出现在meta_value字段,但不知道是哪篇日志的postmeta出了问题?于是再次在飞龙博客数据库搜索"feilong.org/index.php",发现一篇日志的meta_value很长。原来postmeta是日志的meta值,但是meta类型包括好几种,有description,keywords,views,_wp_old_slug,而这个meta值最长的一般是description。于是飞龙直接修改该日志的description值。
7、处理表feilong_posts。这是飞龙博客日志正文里的链接,所以千万谨慎!先检查字段结构(structure)浏览具体数据,发现index.php出现在post_content字段。先仔细"浏览"检查具体值,看有没有不需要修改的日志?飞龙发现guid字段里面也出现"feilong.org/index.php",guid字段Wordpress默认的链接结构。
安全起见飞龙暂时不修改guid字段,只改post_content字段。于是输入如下命令:
update feilong_posts set post_content=REPLACE(post_content,'http://feilong.org/index.php','http://feilong.org')
命令执行结果是:影响列数: 286 (查询花费 1.9448 秒)。咦,怎么不是338项?因为guid字段还没有修订呵呵。飞龙到前台检查,发现绝大部分旧链接已修订好。
想继续修订guid字段值?输入如下命令:
update feilong_posts set guid=REPLACE(guid,'http://feilong.org/index.php','http://feilong.org')
六、飞龙总结:要细心浏览数据库和数据表的结构,以及每个字段值,做严密的分析。
七、最后大家一起看看下面的update命令是否正确,如果发现错误,请指正。
1. update Datatable_name set Field_name=REPLACE(Field_name,Field_namee'tmp'+Field_name+'end ') where file_number=1
2. update Datatable_name set Field_name=REPLACE(Field_name,Field_name,SUBSTRING(Field_name,3,len(Field_name)-2)) where file_number=1
3. update Datatable_name set Field_name=REPLACE(Field_name,Field_name,SUBSTRING(Field_name,1,len(Field_name)-4)) where file_number=2
4. UPDATE table SET memo=REPLACE(memo,'a','A');
5. UPDATE table SET memo=REPLACE(memo,'你','我');
6. update table set Content=replace(Content,'yourblog.org','yongblog.com')
7. update table2 set post_content=replace(post_content,'其它','它们' ) where post_id in (select post_id from table2 where post_content like '%其它%')
八、记得第二次转移空间,飞龙采用WordPress数据备份和恢复的方法,也可以处理wordpress日志的旧网址链接,参见:http://feilong.org/wordpress-datebase-plugins
更新网址:https://feilong.org/phpmyadmin-sql-update-replace
最初发布:20081026 07:59:00 feilong.org 于广州
加入收藏夹,查看更方便。
所在分类: wordpress