The sequence of updated value in MySQL

Posted by Vincent on October 5, 2012

It was a confused phenomenon when I found it.
[cc lang="sql"]CREATE TABLE `tmp_xf` (
`id` int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) not null,
`status` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into tmp_xf(name,status) values ('dbaeyes',1);

select * from tmp_xf;
+----+---------+--------+
| id | name | status |
+----+---------+--------+
| 1 | dbaeyes | 1 |
+----+---------+--------+[/cc]

Phase 1:
[cc lang="sql"]update tmp_xf
set status= if(name='dbaeyes',8,status),
name = 'toby'
where id =1;

+----+------+--------+
| id | name | status |
+----+------+--------+
| 1 | toby | 8 |
+----+------+--------+[/cc]

--restore data
update tmp_xf set status=1,name='dbaeyes' where id = 1;

[cc lang="sql"]update tmp_xf
set name = 'toby',
status= if(name='dbaeyes',8,status)
where id =1;

+----+------+--------+
| id | name | status |
+----+------+--------+
| 1 | toby | 1 |
+----+------+--------+[/cc]

The deifference is the seq of "set values".
It can be considered that MySQL use the updated value for the next action, then the result is different.
It just like
i++ and i=i+1

You can find the detail info by the source code.


This work is licensed under a CC A-S 4.0 International License.