在寫(xiě)UPDATE語(yǔ)句時(shí)捞奕,發(fā)現(xiàn)set多個(gè)字段容燕,并且字段有關(guān)聯(lián)關(guān)系時(shí)琅拌,情況會(huì)不同:
第一種情況如下缨伊,num可以根據(jù)count更新之后的數(shù)據(jù)來(lái)更新,網(wǎng)上搜索“update執(zhí)行順序”进宝,也可以找到很多類(lèi)似的示例:
update test.new_table set count = count + 1, num = count/2 where id = 1;
第二種情況刻坊,是寫(xiě)的比較復(fù)雜的連表更新,排在后面的字段無(wú)法根據(jù)之前已經(jīng)更新的字段來(lái)更新党晋,查看stackoverflow 和 mysql的官方文檔谭胚,發(fā)現(xiàn)描述如下,結(jié)論就是未玻,單表的話(huà)mysql會(huì)有順序灾而,后執(zhí)行的會(huì)用先執(zhí)行的數(shù)據(jù)來(lái)更新,但是多表的話(huà)就不再有該順序:
https://dev.mysql.com/doc/refman/5.6/en/update.html
If you access a column from the table to be updated in an expression, UPDATE uses the current value of the column. For example, the following statement sets col1 to one more than its current value:
UPDATE t1 SET col1 = col1 + 1;
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;
Single-table UPDATE assignments are generally evaluated from left to right.