多表更新的UPDATE优先顺序

时间:2016-01-29 14:50:10

标签: mysql

如果我在SET p.old_email = u.email, u.email = NULL处有UPDATE语句,p.old_email = u.email之前是否u.email = NULL总是会发生?我问的原因是我没有目睹这种行为。

mysql> SHOW TRIGGERS;
UPDATE
Empty set (0.01 sec)

mysql>
mysql> UPDATE
    -> users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> SET p.old_username = NULL, p.username = 'myusername',
    -> p.old_email = NULL, u.email = 'myemail@example.com',
    -> e.record_status = 'inactive', e.date_modified = NOW( ), e.modified_by_id =506836355
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql>
mysql> SELECT p.old_username, p.username, p.old_email, u.email
    -> FROM users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
+--------------+------------+-----------+---------------------+
| old_username | username   | old_email | email               |
+--------------+------------+-----------+---------------------+
| NULL         | myusername | NULL      | myemail@example.com |
+--------------+------------+-----------+---------------------+
1 row in set (0.00 sec)

mysql>
mysql> UPDATE
    -> users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> SET p.old_username = p.username, p.username = NULL
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735Query OK, 1 row affected (0.00 sec);

Rows matched: 1  Changed: 1  Warnings: 0

mysql>
mysql> SELECT p.old_username, p.username, p.old_email, u.email
    -> FROM users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
+--------------+----------+-----------+---------------------+
| old_username | username | old_email | email               |
+--------------+----------+-----------+---------------------+
| myusername   | NULL     | NULL      | myemail@example.com |
+--------------+----------+-----------+---------------------+
1 row in set (0.00 sec)

mysql>
mysql> UPDATE
    -> users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> SET p.old_email = u.email, u.email = NULL
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
Query OK, 1 row affected (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 0

mysql>
mysql> SELECT p.old_username, p.username, p.old_email, u.email
    -> FROM users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
+--------------+----------+-----------+-------+
| old_username | username | old_email | email |
+--------------+----------+-----------+-------+
| myusername   | NULL     | NULL      | NULL  |
+--------------+----------+-----------+-------+
1 row in set (0.00 sec)

mysql>
mysql> UPDATE
    -> users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> SET p.old_username = NULL, p.username = 'myusername',
    -> p.old_email = NULL, u.email = 'myemail@example.com',
    -> e.record_status = 'inactive', e.date_modified = NOW( ), e.modified_by_id =506836355
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3  Changed: 2  Warnings: 0

mysql>
mysql> SELECT p.old_username, p.username, p.old_email, u.email
    -> FROM users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
+--------------+------------+-----------+---------------------+
| old_username | username   | old_email | email               |
+--------------+------------+-----------+---------------------+
| NULL         | myusername | NULL      | myemail@example.com |
+--------------+------------+-----------+---------------------+
1 row in set (0.00 sec)

mysql>
mysql> UPDATE
    -> users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> SET p.old_username = p.username, p.username = NULL,
    -> p.old_email = u.email, u.email = NULL,
    -> e.record_status = 'inactive', e.date_modified = NOW( ), e.modified_by_id =506836355
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3  Changed: 2  Warnings: 0

mysql>
mysql> SELECT p.old_username, p.username, p.old_email, u.email
    -> FROM users u INNER JOIN people p ON p.id = u.id INNER JOIN entities e ON e.id = p.id
    -> WHERE u.id =1753671666 AND u.id !=506836355 AND e.sites_id =2846702735;
+--------------+----------+-----------+-------+
| old_username | username | old_email | email |
+--------------+----------+-----------+-------+
| myusername   | NULL     | NULL      | NULL  |
+--------------+----------+-----------+-------+
1 row in set (0.00 sec)

mysql>
mysql>

1 个答案:

答案 0 :(得分:2)

来自MySQL documentation

  

以下语句中的第二个赋值将col2设置为当前(更新的)col1值,而不是原始col1值。结果是col1和col2具有相同的值。此行为与标准SQL不同。

UPDATE t1 SET col1 = col1 + 1, col2 = col1;
  

单表UPDATE分配通常从左到右进行评估。对于多表更新,无法保证以任何特定顺序执行分配。

您正在执行多表更新,并且列文档分配不会按照文档说明的特定顺序进行。