【问题标题】:move records from one table to another将记录从一张表移动到另一张表
【发布时间】:2017-08-23 08:23:11
【问题描述】:

我有两个表 - postsparchive 具有相同的结构,并且想要将记录从一个表移动到另一个表。

id 列是主键 - 自动增量。

insert into posts select * from parchive where id...

错误:

未捕获的 PDOException:SQLSTATE[23000]:完整性约束违规:1062 键“PRIMARY”的重复条目“70”...

第二次尝试:

insert into posts select date, title, subtitle, intro, story, img, tags,...

除了id之外的所有内容都被选中。

错误:

未捕获的 PDOException:SQLSTATE[21S01]:插入值列表与列列表不匹配:1136 列计数与值计数不匹配

有什么帮助吗?

【问题讨论】:

  • insert into posts (date, title, subtitle, intro, story, img, tags,...) select date, title, subtitle, intro, story, img, tags,... 应该可以正常工作。不过,我想知道为什么您最终会出现重复的唯一索引错误。 You can find the syntax for mysql's INSERT statement here if you want to RTFM
  • @JNevill,它有效,也许你应该发表评论作为答案。
  • 太棒了!我很高兴它奏效了。我已将其添加为答案并进行了详细说明以使其更具答案。

标签: php mysql


【解决方案1】:

根据我上面的评论。如果您只想将数据插入特定字段,则必须在表格之后指定这些字段:

insert into posts (date, title, subtitle, intro, story, img, tags,...) select date, title, subtitle, intro, story, img, tags,... 

我认为可能值得探讨为什么您在插入 ID 时会收到重复的唯一键错误。如果您的 ID 是在您的主表中自动生成的,那么不在您的存档表中自动生成以便 id 匹配是有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2013-04-22
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多