gpt4 book ai didi

php - 覆盖唯一的索引值

转载 作者:行者123 更新时间:2023-11-30 23:23:17 25 4
gpt4 key购买 nike

这就是我现在正在做的(nameUNIQUE):

SELECT * FROM fruits WHERE name='apple';

Check if the query returned any result. If yes, don't do anything. If no, a new value has to be inserted:

INSERT INTO fruits (name) VALUES ('apple');

可以不检查值是否已经存在就将值插入表中,而不是上面的方法吗?如果该名称已存在于表中,将抛出错误,如果不存在,将插入一条新记录。

现在我必须在 for 循环中插入 500 条记录,这会导致 1000 次查询。跳过“已经存在”检查可以吗?

最佳答案

您可以使用忽略功能:

INSERT IGNORE INTO fruits VALUES ('apple')

如果有键违规,它只是跳过这个值

关于php - 覆盖唯一的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14548079/

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