插入到select语句中未复制

时间:2015-03-19 09:09:09

标签: mysql

当从表A复制到表B时,我有两个具有相同结构的表我希望将表A ID和image_name复制到表B中,其中表A和B具有作为主键且自动递增的id。但是ID在表B中有了新的价值。

表格结构:

 `id` int(11) NOT NULL AUTO_INCREMENT,
  `camera_id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `plate` varchar(20) NOT NULL,
  `nread` int(11) DEFAULT NULL,
  `datetime` datetime NOT NULL,
  `millisecs` int(3) NOT NULL,
  `nationality` varchar(3) NOT NULL,
  `image_name` varchar(255) DEFAULT NULL,
  `image` mediumblob,
  `checked` int(1) NOT NULL DEFAULT '0',

$sql = "INSERT INTO $newCamTable (plate, nread, datetime, millisecs, nationality, image_name, image) SELECT plate, nread, datetime, millisecs, nationality, image_name, image FROM $camTable WHERE id=\"$tableEntryId\"";
    logit($sql);

1 个答案:

答案 0 :(得分:0)

更改此行

$sql = "INSERT INTO $newCamTable (plate, nread, datetime, millisecs, nationality, image_name, image) SELECT plate, nread, datetime, millisecs, nationality, image_name, image FROM $camTable WHERE id=\"$tableEntryId\"";

并添加id字段:

$sql = "INSERT INTO $newCamTable (id, plate, nread, datetime, millisecs, nationality, image_name, image) SELECT id,plate, nread, datetime, millisecs, nationality, image_name, image FROM $camTable WHERE id=\"$tableEntryId\"";

这将阻止自动增量功能在表'B'中生成新ID。

<强> BUT

您手动需要管理表'B'中唯一的ID