0

. Hello, My table's columns are: [ID, VALUE, MODIFIED]

How can I make the following update in sql (Its probably easy but i don't achieve it ). i have a int= new_value

UPDATE FROM table SET value = new_value,modified= if (new_value==VALUE) then 0 else 1;
2
  • The question that jumps immediately to mind is, why are you updating a column with a value that hasn't changed? Why not detect the change ahead of time and update value and modified if the value has changed and just update modified if the value hasn't? Commented Aug 6, 2011 at 22:53
  • Because is a multiple row update with multiple values Commented Aug 6, 2011 at 23:03

3 Answers 3

1

Query the original data and use an if-else statement.

if(originalValue == newValue) {
    // Update query
}
else {
    // Update query
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your fast answer Berdon. Thats the way im actually doing. But I was trying to upgrade my program doing it into a single query is that posible?
Hint: compacting your code into incomprehensibility is not an "upgrade"
1

Why not evaluating if it is modified outside the sql:

int isModified = newvalue != VALUE ? 1 : 0;

And in SQL:

UPDATE FROM table SET value = new_value, modified = isModified

1 Comment

I have not the "oldvalue" before doing the query. I just receive the newvalue, and I'm trying to avoid the query for retrieve the old one
1

In regular SQL you can do it like this:

UPDATE tablename 
    SET value = new_value, 
    modified = 
        CASE
            WHEN new_value = value THEN 0
            ELSE 1
        END

But I'm not sure if SQLite supports CASE

2 Comments

SQLite supports CASE but this snipplet is not working either in MySQL or in Android, any idea? Thanks for the answers
Hi, I just modified my answer - sorry I shouldn't have posted without testing it!

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.