mysql的update语句和oracle的update语句的区别

在mysql中,对表进行update,

比如 set id1=id1+10,id2=id1. 这样最终的结果是 id1=id1+10,id2=id1+10 (等于修改后的id1,而不是原来的id1)。

在oracle中,最终结果是id1=id1+10,id2=id1 (还是等于原来的id1的值)。

测试

root@db 14:53:  [test_db]> select * from t_update;
+------+------+
| id1  | id2  |
+------+------+
|    1 |    2 |
|    3 |    4 |
+------+------+
2 rows in set (0.00 sec)

root@db 14:53:  [test_db]> update t_update set id1=id1+10,id2=id1;  -- 这样更改后。列2 的值等于列1的修改后的值了。
Query OK, 2 rows affected (0.15 sec)
Rows matched: 2  Changed: 2  Warnings: 0

root@db 14:54:  [test_db]> select * from t_update;   
+------+------+
| id1  | id2  |
+------+------+
|   11 |   11 |
|   13 |   13 |
+------+------+
2 rows in set (0.00 sec)

root@db 14:54:  [test_db]>
BB@test> select * from t_update;

       ID1        ID2
---------- ----------
         1          2
         3          4

BB@test> update t_update set id1=id1+10,id2=id1;

2 rows updated.

BB@test> commit;

Commit complete.

BB@test> select * from t_update;

       ID1        ID2
---------- ----------
        11          1
        13          3

BB@test>

mysql的官方文档上是这样说的:

1.8.2.2 UPDATE Differences

If you access a column from the table to be updated in an expression, UPDATE uses the current value of the column. The second assignment in the following statement sets col2 to the current (updated) col1 value, not the original col1 value. The result is that col1 and col2 have the same value. This behavior differs from standard SQL.

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

END

发布了754 篇原创文章 · 获赞 31 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/101537269