Skip to content

Instantly share code, notes, and snippets.

@aarsilv
Last active December 24, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarsilv/6827901 to your computer and use it in GitHub Desktop.
Save aarsilv/6827901 to your computer and use it in GitHub Desktop.
Simple example illustrating how in a MySQL UPDATE statement the order of column value pairs in the SET clause matters.
/*
* Beware that the orer of set operations in MySQL are evaluated from left to right for updating a single table,
and in no guarenteed order when updating multiple tables at once through a join.
http://dev.mysql.com/doc/refman/5.7/en/update.html
*/
DROP TABLE IF EXISTS test;
CREATE TABLE test (
num1 int,
num2 int
);
INSERT INTO test (num1, num2)
VALUES (1, 2);
UPDATE test
SET num2 = num1 + 2, num1 = num1 + 1;
SELECT num1, num2
FROM test;
/* 2, 3 */
DROP TABLE IF EXISTS test;
CREATE TABLE test (
num1 int,
num2 int
);
INSERT INTO test (num1, num2)
VALUES (1, 2);
UPDATE test
SET num1 = num1 + 1, num2 = num1 + 2;
SELECT num1, num2
FROM test;
/* 2, 4 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment