Mysql losing 11 records on insert
I download an XML file containing 1048 records, and then I successfully create a table($today) in m开发者_运维技巧y DB, and load the XML data into the MySQL table.
I then run a second script which contains this query:
INSERT INTO
t1
(
modelNumber,
salePrice
)
SELECT modelNumber,salePrice
FROM `'.$today.'`
ON DUPLICATE KEY UPDATE t1.modelNumber=`'.$today.'`.modelNumber,
t1.salePrice=`'.$today.'`.salePrice
");
It works, but I'm losing 11 records. The total count is 1037, while the $today table has the exact amount of records contained in the XML file (1048).
How can I correct this problem?
Runs some queries on the $today to find your 11 duplicates.
The ON DUPLICATE KEY clause will suppress these 11 records.
If there is a duplicate key in your file, you update the old row
ON DUPLICATE KEY UPDATE
Means that if the insert
doesn't work because of a duplicate key, you get the update
mentioned after that line.
There are probably 11 entries that are duplicate keys, and they update
rather then insert
. I would change it to this (a bit of a hack, but the quickest way I can think without any more info to find the culprints)
INSERT INTO
t1
(
modelNumber,
salePrice
)
SELECT modelNumber,salePrice
FROM `'.$today.'`
ON DUPLICATE KEY UPDATE t1.modelNumber=`'.$today.'`.modelNumber,
t1.salePrice= '999999999'
");
Then you can look for entries with that salePrice fo 9999999 , and you at least know what (or even if) duplicate keys you need to look for in your XML
精彩评论