Enrico
Enrico

Reputation: 2037

Update column depending on other column by calculation

seems like a stupid question...

I have a mysql table where I want to modify column A to a number 0 or 1 depending on the condition of another column B

So: if( B > 500 ) A = 1 ELSE A = 0

Column A = INT Column B = DOUBLE

How do you do something like this in sql?

Thanks,

Erik

Upvotes: 1

Views: 52

Answers (1)

John Woo
John Woo

Reputation: 263733

Try the following statement,

UPDATE tableName
SET A = (B > 500)

(B > 500) is a boolean arithmetic in mysql which returns 1 and 0 for true and false , respectively.

You can also use CASE for much more RDBMS friendly,

UPDATE tableName
SET A = CASE WHEN B > 500 THEN 1 ELSE 0 END

Upvotes: 2

Related Questions