6

I need update row in the table only if row exists.

UPDATE table1 SET ctime = now() WHERE id = 112233;

Or with select before

IF EXISTS (SELECT 1 FROM table1 WHERE id = 112233) THEN
   UPDATE table1 SET ctime = now() WHERE id = 112233;
END IF;

Which query better to reduce write operations?

For performance purpose, do I need to do SELECT before UPDATE to check row exists?

1 Answer 1

13

This query:

UPDATE table1
    SET ctime = now()
    WHERE id = 112233;

Does exactly what you want. It updates all rows that match the WHERE condition -- over zero rows.

If you are concerned about performance, create an index on table1(id). If id is a primary key, then it already has an index.

Sign up to request clarification or add additional context in comments.

1 Comment

plus, it will return the count of rows changed, so you can see if update actually updated something... if you care.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.