当从表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);发布于 2015-03-19 10:35:34
更改这一行
$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。
但
您需要手动管理表B中的ID是唯一的。
https://stackoverflow.com/questions/29140573
复制相似问题