gpt4 book ai didi

当两个同时请求时,Mysql 如果存在则失败。如何解决这个问题?

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

我编写了一段代码,用于在仅特定条件不存在时插入查询,但仅当浏览器同时发出两个请求时才会失败。因此数据库中会出现重复条目​​。我在存储过程中使用了它。

示例:

if not exists(select * from tblUser where Email=dEmail and status=2) then
insert into tbluser(Name,Email,Status) values (dName,dEmail,dStatus);
else
Update tblUser set Name=dName where Email=dEmail and status=2;
end if;

请告诉我一个好的解决方案。

最佳答案

您可以向表中添加复合唯一键以防止重复。

ALTER TABLE `tblUser ` ADD UNIQUE `unique_index`(Email,Status);

关于当两个同时请求时,Mysql 如果存在则失败。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48990946/

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