我编写了代码,仅当不存在特定条件时才插入查询,但仅当浏览器同时发出两个请求时,查询才失败。因此,重复条目出现在数据库中。我在存储过程中使用了它。
例:
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 - 两个同时请求时,如果Mysql如果存在失败。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48990946/