我有一个表名" slot_place_box"。它看起来像这样:
我有一个UNIQUE索引(id_place_box + date + hour)。
在PHP中我构建一个SQL请求,它看起来像这样:
$sql_request = "
INSERT INTO media.slot_place_box (id_place_box, date, hour)VALUES (32, '2017-12-10', NULL)ON DUPLICATE KEY UPDATE id_place_box = id_place_box;
SET @id = (SELECT SPB.id_slot_pb FROM media.slot_place_box as SPB WHERE
SPB.id_place_box = 32 AND SPB.date = '2017-12-10' AND SPB.hour IS NULL);
INSERT INTO media.slot_config (id_config, id_slot_pb) VALUES (125, @id);
INSERT INTO media.slot_place_box (id_place_box, date, hour)VALUES (32, '2017-12-11', NULL)ON DUPLICATE KEY UPDATE id_place_box = id_place_box;
SET @id = (SELECT SPB.id_slot_pb FROM media.slot_place_box as SPB WHERE SPB.id_place_box = 32 AND SPB.date = '2017-12-11' AND SPB.hour IS NULL);
INSERT INTO media.slot_config (id_config, id_slot_pb) VALUES (125, @id);
INSERT INTO media.slot_place_box (id_place_box, date, hour)VALUES (32, '2017-12-12', NULL)ON DUPLICATE KEY UPDATE id_place_box = id_place_box;
SET @id = (SELECT SPB.id_slot_pb FROM media.slot_place_box as SPB WHERE SPB.id_place_box = 32 AND SPB.date = '2017-12-12' AND SPB.hour IS NULL);
INSERT INTO media.slot_config (id_config, id_slot_pb) VALUES (125, @id);
//etc
";
我的问题是我的" INSERT ... ON DUPLICATE KEY UPDATE"当我有一个重复的键时,仍然会插入一个新行(=另一个" slot_place_box"在数据库中使用相同的" id_place_box + date + hour",所以我的@id获得了多个结果,脚本错误。它只是在我的表中为我的第一个" INSERT"添加了一行,并且在我有重复行之后什么也没做。
现在这是我的线索:
您有什么想法吗?