On Duplicate Key Update细节,update c=c 和update c=values(c)区别

On Duplicate Key Update细节,update c=c 和update c=values(c)区别

    2020-03-26

前置条件:

create table t1(a int, b int, c int, primary key(a));

下面这段文档的意思?

In assignment value expressions in the ON DUPLICATE KEY UPDATE clause, you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in the ON DUPLICATE KEY UPDATE clause or INSERT statements and returns NULL otherwise. Example:

INSERT INTO t1(a, b, c) VALUES(1,2,3),(4,5,6) ONDUPLICATE KEY UPDATE c = VALUES(b) + VALUES(b);


and

INSERT INTO t1(a, b, c) VALUES(1,2,3),(4,5,6) ONDUPLICATE KEY UPDATE c = b + b;


注意update子句中列引用取值问题:

c = values(b) + values(b)中,b取值为新值;新值,即用户的当前insert语句中前面values中待插入行中的值;

c = b + b,b取值为旧值;旧值,原来行中的值;

所以,c = values(b) + values(b)和c = b + b差别很大。

你可能感兴趣的:(On Duplicate Key Update细节,update c=c 和update c=values(c)区别)