【问题标题】:Insert select auto increment bug插入选择自动增量错误
【发布时间】:2016-12-10 18:04:10
【问题描述】:

我需要一些关于我的 SQL 代码的帮助:

insert into '.$type.' select * from ' .$type.' where effective_id = '.$id

但我收到此代码错误。

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3' for key 'PRIMARY' (SQL: insert into actionplan select * from actionplan where effective_id = 3)

任何想法如何跳过 id 列?我必须在几个表上使用此代码。所以我不能写所有的列。

【问题讨论】:

  • 什么 DBMS?这因特定的 SQL 产品而异。
  • 根据错误信息添加mysql标签

标签: mysql sql select insert auto-increment


【解决方案1】:

您基本上是在问“我如何使用 select * 但不排除其中一列?`

抱歉,select * 不是这个意思。这意味着获取所有列。 SQL 中没有select *-except-for-id 的语法。

如果你想要除一列之外的所有列,那么你必须拼出你想要的所有列。

$sql = "insert into $type (id, col1, col2, col3) 
  select NULL, col1, col2, col3 from $type where effective_id = $id";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多