試圖通過使用INSERT INTO SELECT和連接兩個源表來填充空的MySQL表選擇。想忽略插入定義爲UNIQUE KEY基於兩個目標表列重複行的,但出於某種原因,複製基於這兩個列的行尚未插入。mySQL的INSERT忽略與唯一鍵不工作
目標表的定義:
CREATE TABLE `item` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10) unsigned NOT NULL,
`account_id` int(10) unsigned NOT NULL,
`creation_date` datetime NOT NULL,
`modification_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_item` (`item_id`,`account_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
插入查詢:
INSERT IGNORE INTO item
(id,
item_id,
account_id,
creation_date,
modification_date)
SELECT tblItem.ID,
tblItem.itemID,
tblOrder.accID,
tblItem.itemTime,
'0000-00-00 00:00:00'
FROM tblItem
INNER JOIN tblOrder
ON tblItem.orderID = tblOrder.ID
重複行被插入項目類似以下內容:
id item_id account_id creation_date modification_date
2587 0 2 11/19/11 2:43 0000-00-00 00:00:00
2575 0 1120 11/17/11 19:32 0000-00-00 00:00:00
2575 0 1120 11/17/11 19:32 0000-00-00 00:00:00
382 60 193 0000-00-00 00:00:00 0000-00-00 00:00:00
941 95 916 10/28/11 15:52 0000-00-00 00:00:00
369 108 1 0000-00-00 00:00:00 0000-00-00 00:00:00
373 108 2 0000-00-00 00:00:00 0000-00-00 00:00:00
378 109 2 0000-00-00 00:00:00 0000-00-00 00:00:00
378 109 2 0000-00-00 00:00:00 0000-00-00 00:00:00
我在想什麼?
在此先感謝!
你確定你想加入'tblOrder'到'tblItem'而不是其他方式嗎? – Kermit
有區別嗎?它會被編碼如下: FROM tblItem INNER JOIN tblOrder ON tblOrder.ID = tblItem.orderID – pseudonymo