-2

In my database I have a updateinfo with timestamp etc. which should updated every time the values has changes/update.

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

Now I ask myself how I can do it easier and faster. Normally MySQL doesn't update something if the value has not changed. But what about my updateinfos. If I add them MySQL will update them every time. How can I do it that MySQL only sets 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.

3
  • please, format your code with correct syntax Commented Mar 14, 2017 at 5:26
  • you mean if a or b has new data to update than udateinfo will update? Commented Mar 14, 2017 at 5:38
  • yes but also then set and update the updateinfo Commented Mar 14, 2017 at 6:02

2 Answers 2

1

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'");
}
Sign up to request clarification or add additional context in comments.

9 Comments

Correct me if I misinterpreted OP's question
i think in this way you everytime update the updateinfo, mysql will alway sucess this if-part!?
I just copied what you had given, you can of course make the values dynamic.
yes of course ;-) but i think that is not the way to do what i want!?
If so, please edit your question so that we may understand it better. From what I understood, you will first update the a and b, then if they are successful, you will update Updateinfo, am I correct?
|
0

here my solution in php style

UPDATE software_config SET modifiedby=IF(STRCMP(main, '$main')=0 AND STRCMP(setting, '$setting')=0 AND STRCMP(value, '$value')=0, modifiedby, $new_value), main='$main', setting='$setting', value='$value' WHERE id=$id"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.