Mysql 插入时丢失 11 条记录
我下载了一个包含 1048 条记录的 XML 文件,然后我在数据库中成功创建了一个表($today),并将 XML 数据加载到 MySQL 表中。
然后,我运行第二个脚本,其中包含此查询:
INSERT INTO
t1
(
modelNumber,
salePrice
)
SELECT modelNumber,salePrice
FROM `'.$today.'`
ON DUPLICATE KEY UPDATE t1.modelNumber=`'.$today.'`.modelNumber,
t1.salePrice=`'.$today.'`.salePrice
");
它有效,但我丢失了 11 条记录。总计数为 1037,而 $today 表具有 XML 文件中包含的准确记录数量 (1048)。
我该如何纠正这个问题?
I download an XML file containing 1048 records, and then I successfully create a table($today) in my 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对 $today 运行一些查询以查找 11 个重复项。
ON DUPLICATE KEY 子句将抑制这 11 条记录。
Runs some queries on the $today to find your 11 duplicates.
The ON DUPLICATE KEY clause will suppress these 11 records.
如果文件中存在重复的键,则更新旧行
意味着如果由于重复的键而导致
insert
不起作用,您将得到后面提到的update
那条线。可能有 11 个条目是重复键,并且它们
更新
而不是插入
。我会将其更改为这样(有点黑客,但我可以想到的最快方法,无需任何更多信息即可找到罪魁祸首)然后您可以查找带有 salePrice fo 9999999 的条目,并且您至少知道什么(甚至if) 您需要在 XML 中查找重复的键
If there is a duplicate key in your file, you update the old row
Means that if the
insert
doesn't work because of a duplicate key, you get theupdate
mentioned after that line.There are probably 11 entries that are duplicate keys, and they
update
rather theninsert
. 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)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