dongyu1918 2017-03-14 05:25
浏览 26

如果更改了值,则更新mysql db并更改其他值

in my database i have a updateinfo with timestamp etc. wich should updated everytime the values has changes/update.

actually i check at first if there has changed value (old) and value (new) only if its, i add the update value with new updateinfos.

Now i ask myself how i can do it easier and faster. Normaly mysql dont update somethink if the value has not changes. But whats about my updateinfos. If i add them mysql will update them everytime. How can i do it that mysql only set the new updateinfos if there is really a changed value?

For Example

mysql SET a=1, b=2, Updateinfo='TIMESTAMP+IP'

What i looking for is a kind like this

mysql SET a=1, b=2, (Updateinfo='Timestamp+IP' ONLY IF a or b has updated)

So is there a way to add the field Updateinfo='Timestamp...' to the query if there is a change by one of the SET a=1, b=2?

Hope i could explain what i'm looking for.

  • 写回答

1条回答 默认 最新

  • duansha7453 2017-03-14 05:33
    关注

    Something like this? It's only a pseudo-code, but you get the idea

    run_query("UPDATE table SET a=1, b=2");
    if(success == run_query){
        run_query("UPDATE table SET Updateinfo='Timestamp+IP' WHERE a=1 AND b=2");
    } else {
        //some other things...
    }
    

    Revise #1

    $a = 'somevalue';
    $b = 'somevalue2';
    
    $result = fetch -> run_query('SELECT a, b FROM table');
    $existingA = $result["a"];
    $existingB = $result["b"];
    
    if(($existingA != $a) || ($existingB != $b)){
        run_query("UPDATE table SET a=$a, b=$b, Updateinfo='Timestamp+IP'");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题