Skip to content

Instantly share code, notes, and snippets.

@EdoardoVignati
Created November 2, 2020 18:51
Show Gist options
  • Save EdoardoVignati/0eede9ba80c9339a29842f61c1e5105f to your computer and use it in GitHub Desktop.
Save EdoardoVignati/0eede9ba80c9339a29842f61c1e5105f to your computer and use it in GitHub Desktop.
Updates all rows based on a condition over the same table in MySql
UPDATE tbl
SET field_to_update = CASE
WHEN (SELECT COUNT(*) FROM (SELECT * FROM tbl) AS copy WHERE copy.field=<condition> HAVING COUNT(*)>0) THEN "this_value_if_true"
ELSE field_to_update
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment