我正在wordpress数据库中迁移一些数据,并在最后一部分卡住了。
我有三个表,一个是官方WordPress表,两个是临时表。这是他们的结构:
wp_term_relationships:object_id,term_taxonomy_id,term_order
old_terms:term_id,slug
new_terms:term_id,slug
slug在old_terms和new_terms表之间匹配。我需要通过wp_term_relationships并使用以下规则创建新记录:
object_id = wp_term_relationships.object_id
term_taxonomy_id = new_terms.term_id WHERE new_terms.slug = old_terms.slug
term_order = wp_term_relationships.term_order
我已经尝试了几十种不同的sql调用,但似乎无法想出这一个。这是我试过的最后一个
INSERT INTO wp_term_relationships ( object_id, term_taxonomy_id, term_order )
SELECT object_id, new_terms.term_id, term_order
WHERE term_id = old_terms.term_id ON new_terms.slug = old_terms.slug
我已经有一段时间了,并没有取得任何进展。有什么想法吗?
答案 0 :(得分:0)
好吧,经过努力,我终于能够使用以下代码。
INSERT INTO wp_term_relationships ( object_id, term_taxonomy_id, term_order )
SELECT c.object_id, a.tax_term_id, c.term_order
FROM new_terms AS a
LEFT JOIN old_terms AS b ON a.slug = b.slug
LEFT JOIN wp_term_relationships AS c ON b.term_id = c.term_taxonomy_id
WHERE term_taxonomy_id IN ( SELECT term_id FROM old_terms )
如果有人碰到类似的东西,请随时联系。我觉得我现在比以前更好地处理现在发生的事情的机制。