Trying to populate an empty MySQL table by using INSERT INTO SELECT and joining two source tables. Would like to IGNORE insertion of duplicate rows based on two destination table columns defined as UNIQUE KEY, but for some reason, duplicate rows based on these two columns are still being inserted.
Destination table definition:
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 query:
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
Duplicate rows are being inserted into item similar to the following:
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
What am I missing?
Thanks in advance!
tblOrder
totblItem
not the other way around? – Kermit