问题:同一台服务器里的两个wordpress数据库,我要把名为temp数据库里的wp_posts的所有文章 全部复制到名为cuwen数据库wp_posts里,cuwen里已经有文章,复制过来的文章当做新加,不要删除或覆盖老数据文章。
使用下面的 SQL 脚本,将一台服务器上的 temp 数据库中的 wp_posts 表内容,完整复制到已有内容的 cuwen.wp_posts 表中,并确保所有复制过来的文章在 cuwen 数据库中是作为“新文章”插入(不会覆盖原有文章):
安全复制文章 SQL 语句(适用于 WordPress 标准结构)
INSERT INTO cuwen.wp_posts (
post_author, post_date, post_date_gmt,
post_content, post_title, post_excerpt,
post_status, comment_status, ping_status,
post_password, post_name, to_ping, pinged,
post_modified, post_modified_gmt,
post_content_filtered, post_parent,
guid, menu_order, post_type, post_mime_type, comment_count
)
SELECT
post_author, post_date, post_date_gmt,
post_content, post_title, post_excerpt,
post_status, comment_status, ping_status,
post_password, post_name, to_ping, pinged,
post_modified, post_modified_gmt,
post_content_filtered, 0 AS post_parent, -- 置为0避免引用不存在的父文章
guid, menu_order, post_type, post_mime_type, comment_count
FROM temp.wp_posts
WHERE post_type IN ('post', 'page'); -- 只复制文章和页面(如有其他类型可扩展)
说明:
1、ID 字段会自动增长(前提是 ID 为自增主键,标准 WordPress 表结构是这样)。
2、不复制 ID 字段,是为了让 MySQL 为 cuwen.wp_posts 自动分配新文章 ID,避免覆盖。
3、如果你使用了自定义文章类型或需要全部类型,去掉 WHERE post_type IN (…) 条件即可。
4、post_parent 设置为 0,防止引用原数据库中不存在的父文章。