Mysql 插入时丢失 11 条记录

发布于 2024-11-23 16:07:29 字数 445 浏览 4 评论 0原文

我下载了一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-11-30 16:07:29

对 $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.

冷月断魂刀 2024-11-30 16:07:29

如果文件中存在重复的键,则更新旧行

ON DUPLICATE KEY UPDATE

意味着如果由于重复的键而导致 insert 不起作用,您将得到后面提到的 update那条线。

可能有 11 个条目是重复键,并且它们更新而不是插入。我会将其更改为这样(有点黑客,但我可以想到的最快方法,无需任何更多信息即可找到罪魁祸首)

INSERT INTO
        t1
(
    modelNumber,
    salePrice

    )
SELECT modelNumber,salePrice
FROM `'.$today.'`

ON DUPLICATE KEY UPDATE t1.modelNumber=`'.$today.'`.modelNumber,
 t1.salePrice= '999999999'
");

然后您可以查找带有 salePrice fo 9999999 的条目,并且您至少知道什么(甚至if) 您需要在 XML 中查找重复的键

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文