用什么SQL查询做一个简单的查找和替换
-
-
如果您不熟悉查询,请尝试使用"搜索和替换"插件http://wordpress.org/extend/plugins/search-and-replace/If you're not familiar with queries try the Search and Replace plugin, http://wordpress.org/extend/plugins/search-and-replace/
- 0
- 2011-01-26
- t31os
-
为什么不使用WordPress功能来更新URL? http://codex.wordpress.org/Moving_WordPress详细介绍了所有内容Why not use WordPress functionality to update the URL? http://codex.wordpress.org/Moving_WordPress details everything
- 0
- 2014-11-18
- Alex Older
-
有一个插件.如果您愿意,它可以方便地使用后端,并且还可以替换后内容和某些其他字段中的URL:https://wordpress.org/plugins/better-search-replace/There is a plugin for this. It allows comfortable use of the backend and also replaces the URL in the post-content and some other fields, if you want to: https://wordpress.org/plugins/better-search-replace/
- 0
- 2015-07-23
- simonthesorcerer
-
7 个回答
- 投票数
-
- 2011-01-25
保存URL的表是wp_options.您应该在使用您网站网址的列上进行更新:
UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "siteurl" UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "home"
我可能会丢失一些值,但是每当您再次执行此查找/替换过程时,您都会注意到应该更新的值和表并将它们添加到此脚本中.
WordPress Codex提供了有关如何更改网站URL的不错的指南,也许对您来说更方便:更改网站URL
The table where your URL is saved is wp_options. You should do an update on the columns that use the URL for your site:
UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "siteurl" UPDATE TABLE wp_options SET option_value = "new domain" WHERE option_name = "home"
I might be missing some value, but whenever you do this find/replace process again, you can notice the values and tables that should be updated and add them to this script.
WordPress Codex has a nice guide on how to change a site URL, maybe that's even handier for you: Changing the Site URL
-
有没有办法在整个数据库上进行查找/替换?换句话说...例如,我注意到我需要在包括媒体库在内的许多不同位置中替换URL....如果实际上每个字段的整个数据库都存在查找/替换,那么这将解决问题.谢谢你的帮助is there not a way to do a find/replace on the entire database? In other words... I noticed for example that I need to replace the URLs in a bunch of different locations including the media library.... If there was a find/replace for the entire database essentially for every field then this would solve the problem. Thanks for your help
- 1
- 2011-01-25
- NetConstructor.com
-
查看我在答案中添加的新链接.我认为那是要走的路.Check out that new link I added on the answer. I think that would be the way to go.
- 0
- 2011-01-25
- Fernando Briano
-
这不适用于序列化数据.它可能会完全破坏某些主题配置.This will not work for serialized data. It might completely break some theme configuration.
- 2
- 2017-10-17
- Christian Lescuyer
-
- 2011-01-25
最适合做的选择,发布,发布内容和发布meta:
UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com'); UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');
另请参见使用phpMyAdmin和MySQL搜索数据| Packt Publishing.并且搜索RegEx 是一个很好的WP插件,能够在所有帖子和页面中搜索并替换为Grep.
2015年6月16日更新::使用下句话中链接的工具要好得多,因为在数据库转储中使用上述的简单查找/替换操作会破坏序列化的数据./em>请参见interconnectit.com WordPress序列化的PHP搜索替换工具.这样,您就不会破坏序列化的数据,也不需要在帖子内容上运行RegEx,因为互连脚本会随处更改URL.我一直使用该工具将网站迁移到不同的域,或者只是从http到https进行全局更改,以强制不使用插件的SSL,并更改内容中的所有URL,以防止不安全的元素错误.
Best to do options, posts, post content and post meta:
UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com'); UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');
Also see Searching Data using phpMyAdmin and MySQL | Packt Publishing. And Search RegEx is a good WP plugin to be able to search and replace with Grep through all posts and pages.
Update 6/16/2015: Using the tool linked in the next sentence is much better, as a simple find/replace as above in a database dump will break serialized data. See interconnectit.com WordPress Serialized PHP Search Replace Tool. This way, you don't break serialized data and won't need to run RegEx on post content, as the interconnect script changes URLs everywhere. I use that tool all the time to migrate sites to different domains, or to simply do a global change from http to https to force SSL without plugins and change all URLs in content to prevent insecure element errors.
-
永远不要更改GUID-即使转到新域.它用于唯一标识帖子,因为如果将帖子导出/导入到新数据库中,则ID可能会更改.一方面,RSS阅读器将使用GUID判断是否已阅读特定文章.更改GUID会有效地重新发布您的所有文章.Don't ever change the guid - even if going to a new domain. It's used to uniquely ID the post as the ID can change if the posts are being exported/imported into a new database. For one thing, RSS readers will use the GUID to tell if a particular article has been read or not. Changing the guid will effectively republish all your articles.
- 2
- 2012-06-16
- Taylor Dewey
-
@taylordewey说:"永远不要改变向导……"垃圾.@taylordewey said: "Don't ever change the guid..." Rubbish.
- 0
- 2012-06-17
- markratledge
-
@songdogtech Care解释*为什么*是垃圾?@songdogtech Care to explain *why* it is rubbish?
- 1
- 2013-03-06
- shea
-
如果要更改域,则根本没有选择,而必须更改GUID.RSS阅读器的影响是最低的价格.If you're changing domains, you simply don't have a choice and have to change GUIDs. Fallout from RSS readers is a minimal price to pay.
- 0
- 2015-11-06
- markratledge
-
为什么一个人要更改GUID?Why would one _have_ to change the GUID?
- 1
- 2016-08-11
- kaiser
-
尽管GUID包含url,但在引用帖子时,它们并不用于创建链接(例如,在菜单中).因此,_不_更改它们是完全可以接受的Despite GUID's containing a url, they aren't used to create links (for example in menu's) when referencing posts. And so _not_ changing them is perfectly acceptable
- 0
- 2017-05-27
- Chris
-
这不适用于序列化数据.它可能会完全破坏某些主题配置.确实使用诸如互连/IT之类的工具.This will not work for serialized data. It might completely break some theme configuration. Indeed use a tool such as Interconnect/IT’s.
- 1
- 2017-10-17
- Christian Lescuyer
-
- 2011-08-02
这是我使用的出色的脚本,可与WP用于存储选项的序列化数组完美地结合使用.完成操作后,只需确保将其从远程服务器中删除即可,因为它存在巨大的安全风险.
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
This is a great drop-in script that I use and it works beautifully with the serialized arrays that WP uses to store options. Just make sure to delete it from your remote server when you're done because it's a HUGE security risk.
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
-
我不知道为什么我被-1.该脚本比SQL语句好得多.反馈好吗?I don't know why I was -1'd. This script is much better than a SQL statement. Feedback please?
- 2
- 2011-08-03
- lancemonotone
-
一个不了解sql的人可以使用的工具,使那些用sql编写的人感到沮丧a tool that people who dont know sql can use, is upsetting to those who write in sql
- 1
- 2013-06-23
- Jon
-
- 2016-04-27
为此,我使用 WP-CLI ,因为我发现它最简单负责序列化数据.
wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid
还有一个选项可以将您的更改写入SQL文件,而不是操作实际的数据库:
wp search-replace foo bar --export=database.sql
For this I use WP-CLI because I find it the easiest and it takes care of serialized data.
wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid
There is also an option that writes your changes into an SQL file instead of manipulating the actual database:
wp search-replace foo bar --export=database.sql
-
迄今为止最强大,最快的解决方案.wp-cli再次节省了一天的时间by far the most robust and fastest solution. wp-cli saves the day once again
- 1
- 2018-06-19
- ryanrain
-
- 2011-01-25
您不必这样做,可以使用相对路径.
链接而不是subdomain.soemthing.com/image.jpg时-例如使用/image.jpg
像这样,您一开始就不会遇到问题.
否则,对于mysql更新语句,您可以使用
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
you do not have to do this , you can use relative paths.
when you are linking something instead of subdomain.soemthing.com/image.jpg - use /image.jpg for example
like this you won't face the problem in the first place.
otherwise for a mysql update statement you can use
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
-
谢谢...是的,下次我会做.SQL语句是否在整个数据库(包括所有表)上进行查找替换?Thanks... yeah I will do that next time. The SQL statement does a find replacement on the entire database (including all tables)?
- 0
- 2011-01-25
- NetConstructor.com
-
@ NetConstructor.commireille上面给您的SQL语句是用于替换特定表中特定字段中的字符串的通用MySQL命令.如果您尝试完全按照编写的方式运行此语句,那么它将无法正常工作.为了使此命令生效,您需要将TABLE_NAME和FIELD_NAME更改为WordPress使用的真实字段和表.@NetConstructor.com The SQL statement mireille gave you above is the generic MySQL command for replacing a string in a specific field in a specific table. If you tried running this statement exactly as it was written, it wouldn't work. For this command to work, you'd need to change the TABLE_NAME & FIELD_NAME to a real field and table used by WordPress.
- 0
- 2011-01-26
- Manzabar
-
还要注意,即使您希望使用相对路径,wordpress的许多部分也倾向于自动插入完整路径.要使此插件真正起作用,请使用以下插件:https://wordpress.org/plugins/root-relative-urls/非常有用Note also that even if you desire to use relative paths, lots of parts of wordpress tend to auto-insert full paths. To really get this working a plugin like: https://wordpress.org/plugins/root-relative-urls/ is very, very helpful
- 0
- 2014-09-15
- benz001
-
- 2011-11-24
要更改我们经常需要的wordpress域,可能是使站点从localhost运行:这是更新查询的完整列表:
UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com'); UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_links SET link_url = replace(link_url, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_links SET link_image = replace(link_image, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_usermeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com'); /*UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl' OR option_name = 'widget_text' OR option_name = 'dashboard_widget_options';*/ UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com');
- 如果需要,我们还必须添加其他不是WP默认的表.
更新:搜索替换数据库版本3.1.0是面向开发人员的用户友好的前端工具,可让您执行数据库范围的搜索/替换操作,而不会损坏PHP序列化的字符串或对象.
To change the wordpress domain what we often need, may be to make the site live from localhost: This is a complete list of update queries:
UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com'); UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_links SET link_url = replace(link_url, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_links SET link_image = replace(link_image, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com'); UPDATE wp_usermeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com'); /*UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl' OR option_name = 'widget_text' OR option_name = 'dashboard_widget_options';*/ UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com');
- We have to also add other tables which are not default with WP if necessary.
UPDATE: Search Replace DB version 3.1.0 is a user-friendly, front-end tool for developers, that allows you to carry out database wide search/replace actions, that don't damage PHP serialized strings or objects.
-
这不适用于序列化数据.它可能会完全破坏主题配置.This will not work for serialized data. It might completely break theme configuration.
- 2
- 2017-10-17
- Christian Lescuyer
-
- 2015-05-06
实际上,您不必仅在主题中的wp_config和functions.php文件中进行一些调整即可使用SQL查询.在Wordpress Codex中查看此主题: https://codex.wordpress.org/Changing_The_Site_URL
Actually, you don't have to use a SQL query just some adjustments in wp_config and functions.php file in your theme. Check out this topic in Wordpress Codex: https://codex.wordpress.org/Changing_The_Site_URL
-
这是不对的.不幸的是,WordPress将一些URL作为文本存储在数据库中.This isn't correct. WordPress unfortunately stores some URL in the database as text.
- 1
- 2015-09-30
- s_ha_dum
每当我创建一个新网站时,我都会首先在诸如" stage.domain-name.com"之类的子域上创建一个临时站点.
一切正常后,我导出数据库,在notepad ++中打开它,并查找/替换" subdomain.domain-name.com",然后将其替换为" domain-name.com" ...最后,我导入了它进入实时网站的新数据库.
我的问题是...如果我只想使用phpmyadmin在整个数据库上执行此简单查找/替换操作,则需要运行什么SQL查询?
-CH