尝试通过使用INSERT INTO SELECT并连接两个源表来填充一个空的 MySQL 表。想要忽略基于定义为UNIQUE KEY的两个目标表列的重复行的插入,但由于某种原因,仍在插入基于这两个列的重复行。
目标表定义:
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
我错过了什么?
提前致谢!