【问题标题】:Mysql - insert or update and get old valueMysql - 插入或更新并获取旧值
【发布时间】:2013-09-23 02:02:40
【问题描述】:

我有一个带有 3 列 A、B、C 和 A 和 B 的唯一表的 SomeTable

如果 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...
   }    
}

这意味着对 MySQL 的 3 次调用......

有没有办法通过触发器、过程或“魔术”查询来做到这一点?

【问题讨论】:

标签: php mysql insert-update


【解决方案1】:

您可以通过两个查询来做到这一点:

  1. 尝试SELECT 旧值。如果存在,则在第 2 步后返回
  2. UPDATEINSERT 视需要而定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 2013-03-06
    • 2015-04-25
    • 1970-01-01
    • 2017-03-16
    • 2018-03-12
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多