MySQL UPDATE Tutorials

The UPDATE statement is used to update existing records in a table.

Replace/Delete part of a string

Replace “http://” with “https://” in users.website field

UPDATE users SET website=REPLACE(website,'http://','https://')

Delete a part

UPDATE users SET website=REPLACE(website,'http://example.com','')

UPDATE query with condition

UPDATE account
SET validated = 0
WHERE email IS null

Update Column +1

If the column is a number, you can set it like this.

UPDATE table
SET column_name = column_name + 1
WHERE id = <any id>

UPDATE query based on SELECT query

We have 2 tables, account and user_info and want to update account.active to true if user_info.country is “USA”.

UPDATE account
SET account.active = 1
FROM (SELECT id, country FROM user_info)
WHERE account.id = user_info.id AND user_info.country = 'USA'

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close