Bug #3675 UPDATE on multiple tables doesn't work correctly when inner joining by a comma

Submitted: 6 May 2004 9:33 Modified: 6 May 2004 19:32
Reporter: Mike Zhang Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.0+4.1 OS:Windows (Windows 2000)
Assigned to: Dean Ellis CPU Architecture:Any

[6 May 2004 9:33] Mike Zhang
Description:
UPDATE on two tables doesn't work correctly when inner joining by a comma.
As the manual said,
--------------------------------------------------------------
Starting with MySQL 4.0.4, you can also perform UPDATE operations that cover multiple tables: 

UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

The example shows an inner join using the comma operator, but multiple-table UPDATE statements can use any type of join allowed in SELECT statements, such as LEFT JOIN. 
--------------------------------------------------------------
However, multiple tables update operation seems to behaviour questionably.
I just wanna swap the value of name column in two tables. I find "update tab1, tab2" operates differently with "update tab2, tab1".

update tab1, tab2 set tab1.name=tab2.name, tab2.name=tab1.name where tab1.id=tab2.id

update tab2, tab1 set tab1.name=tab2.name,
tab2.name=tab1.name where tab1.id=tab2.id

How to repeat:
mysql> create table tab1(id int, name int);
Query OK, 0 rows affected (0.09 sec)

mysql> create table tab2(id int, name int);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into tab1 values(1, 9), (2, 13);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into tab2 values(1, 27), (2, 5);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> update tab1, tab2 set tab1.name=tab2.name, tab2.name=tab1.name where tab1
.id=tab2.id;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 0

mysql> select * from tab1;
+------+------+
| id   | name |
+------+------+
|    1 |   27 |
|    2 |    5 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from tab2;
+------+------+
| id   | name |
+------+------+
|    1 |   27 |
|    2 |    5 |
+------+------+
2 rows in set (0.00 sec)

mysql> delete from tab1;
Query OK, 2 rows affected (0.00 sec)

mysql> insert into tab1 values(1, 9), (2, 13);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> update tab2, tab1 set tab1.name=tab2.name, tab2.name=tab1.name where tab1
.id=tab2.id;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 0

mysql> select * from tab1;
+------+------+
| id   | name |
+------+------+
|    1 |    9 |
|    2 |   13 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from tab2;
+------+------+
| id   | name |
+------+------+
|    1 |    9 |
|    2 |   13 |
+------+------+
2 rows in set (0.00 sec)
[6 May 2004 19:32] Dean Ellis
For a single-table UPDATE the SET is processed left to right, so for example:

UPDATE t1 SET a = b, b = a;

would set both a and b to the value b had originally; by the time "b=a" is executed, a has already been set to the value of b.

You are seeing the same thing here, only the tables are also being updated left-to-right per the order in the table list.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.