我有一张A,B,C三列的桌子,A和B上有一个唯一的
从客户端,如果A、B已经存在,我会传递一些值插入或更新C。
我想知道服务器是否对每个值都进行了更新或插入,如果是更新,我想知道旧值C。。。。
我现在正在和

While values to insert {
   Insert A,B,C
   if success memorizing the value continue the while
   else if the sql error is "duplicate key" {
       select old value of C for A,B
       update C for A,B...
   }
}

这意味着3次调用MySQL。。。。
有没有一种方法可以通过触发器、过程或“魔术”查询来实现这一点?

最佳答案

您可以通过两个查询来完成此操作:
尝试SELECT旧值。如果存在,则在步骤2之后返回。
UPDATEINSERT根据需要

关于php - MySQL-插入或更新并获取旧值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18947001/

10-14 13:43
查看更多