MySQL Update field of set IF NULL or other value

I think that’s it’s possible for you to do this using an IF statement. The IF statement takes 3 parameters when you’re using it: the expression, value if true, value if false

So in your case, you could probably write your queries in one go like the following:

UPDATE Table1 
SET 
    field1 = field1 + 1, 
    field2 = NOW(),
    field3 = IF(field3 < '2011-00-00 00:00:00' OR field3 IS NULL, NOW(), field3)
WHERE id = $id;

This way, if expression is true, then field3 will be NOW() and otherwise, it’ll remain as it was.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)