Ask Your Question
4

How can I modify mySQL only if a field has a specific value, but keep it unchanged otherwise?

asked 2023-05-26 22:28:24 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-26 22:57:02 +0000

devzero gravatar image

You can use a conditional statement with the UPDATE query to update the specific field only if it has a certain value, and keep it unchanged otherwise.

For example, the following query updates the 'status' field to 'completed' only if the 'id' is equal to 1, and keeps it unchanged for all other records:

UPDATE table_name
SET status = 'completed'
WHERE id = 1 AND status != 'completed';

In this query, the AND status != 'completed' condition ensures that the status field is not updated if it already has the value 'completed'.

Similarly, you can use different conditions to update specific fields based on other criteria.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-26 22:28:24 +0000

Seen: 11 times

Last updated: May 26 '23