1062关键'PRIMARY'重复输入'234'

时间:2017-06-03 10:56:54

标签: mysql wordpress

我正在从drupal导入帖子到WP,我想在运行脚本时也有类别和标签,它导入所有的东西,但不是类别和标签,我得到这个错误:

Query:

INSERT INTO wordpress.wp_term_taxonomy (term_taxonomy_id, term_id, taxonomy, description, parent)
SELECT DISTINCT
    d.tid, d.tid 'term_id', 'post_tag', d.description 'description',
    h.parent 'parent'
FROM drupal.term_data d
INNER JOIN drupal.term_hierarchy h USING(tid)
INNER JOIN drupal.term_node n USING(tid)
WHERE (1); 
  

错误:SQLSTATE [23000]:完整性约束违规:1062密钥'PRIMARY'的重复条目'234'

我删除了我的node_term表但没有帮助。

1 个答案:

答案 0 :(得分:0)

您显然正在插入表中已有的值 - 或者,您的查询的值为" 234"主键两次。

您可以通过执行单独的子查询来检查第二个条件:

select pk, count(*)
from (<your select here>) t
group by pk
having count(*) > 1;

您可以使用INSERT IGNOREON DUPLICATE KEY UPDATE解决这两个问题。我更喜欢后者:

INSERT INTO wordpress.wp_term_taxonomy (term_taxonomy_id, term_id, taxonomy, description, parent)
    SELECT DISTINCT d.tid, d.tid, 'post_tag', d.description,h.parent
    FROM drupal.term_data d INNER JOIN
         drupal.term_hierarchy h USING( tid) INNER JOIN
         drupal.term_node n USING( tid)
    WHERE (1)
    ON DUPLICATE KEY UPDATE term_taxonomy_id = VALUES(term_taxonomy_id);