gpt4 book ai didi

php - SQL拒绝批量插入,因为单行重复

转载 作者:行者123 更新时间:2023-11-29 04:22:48 26 4
gpt4 key购买 nike

我不知道正确的术语,但我所说的“批量插入”是指......

INSERT INTO `table` (`column1`, `column2`, `column3`) VALUES 
("value1", "value2", "value3"),
("value4", "value5", "value6"),
("value7", "value8", "value9")

我们在单个查询中插入多行以节省资源。

我们在这张表上设置了一个组合键,以避免得到重复的记录。问题是,当存在重复记录时,它会拒绝整个“批处理”,因此在上面的示例中,如果 column1 列中已经存在“value1”,它不会插入任何 3行。

我们将 PHP 与 mysql_ 函数一起使用(是的,我知道它们已被弃用,请一次解决一个问题)

所以我的问题是,有没有办法一次插入多行,如果出现重复行,它仍会“批量”插入其余行?

感谢您的帮助。

最佳答案

试试ignore关键字

INSERT IGNORE INTO `table` ...

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error occurs. Ignored errors may generate warnings instead, although duplicate-key errors do not.

关于php - SQL拒绝批量插入,因为单行重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com