Last active
December 24, 2015 16:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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