# [KB]关于 TiDB v4.0 和 v5.0 多表 Update 交换数值结果不一致

## Description

自 v5.0.0 之后，TiDB 同时 Update 多个字段时，执行 SQL 的行为发生改变

```sql
-- 版本：TiDB 4.0.x

mysql> insert into test values ('a','b');
Query OK, 1 row affected (0.04 sec)

mysql> select * from test;
+---------+---------+
| col1    | col2    |
+---------+---------+
| a       | b       |
+---------+---------+
1 row in set (0.01 sec)

mysql> update test set col1=col2,col2=col1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

-- 只会生效 col1 修改 col2 值，所以查询结果都为值都为 b.
mysql> select * from test;
+---------+---------+
| col1    | col2    |
+---------+---------+
| b       | b       |
+---------+---------+
1 row in set (0.00 sec)

-- 版本：TiDB 5.0.0 以后

mysql> create table test(col1 varchar(50),col2 varchar(50));
Query OK, 0 rows affected (0.12 sec)

mysql> insert into test values ('a','b');
Query OK, 1 row affected (0.02 sec)

mysql> select * from test;
+---------+---------+
| col1    | col2    |
+---------+---------+
| a       | b       |
+---------+---------+
1 row in set (0.01 sec)


-- 会将 col1 和 col2 值进行更新，结果为 col1=b,col2=a;

mysql> update test set col1=col2,col2=col1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+---------+---------+
| col1    | col2    |
+---------+---------+
| b       | a       |
+---------+---------+
1 row in set (0.00 sec)
```

## Root Cause

以前版本为了兼容 mysql，在同一 update 语句更新多个字段时，后面字段的更新依赖前面字段的更新结果，详情建议 MySQL issue [Bug #100527](https://bugs.mysql.com/bug.php?id=100527&thanks=2&notify=67)

在 v5.0.0 以后和 SQL 标准保持一致，修改了 SQL 行为。

Github Issue：[https://github.com/pingcap/tidb/issues/19137](https://github.com/pingcap/tidb/issues/19137)

PR：[https://github.com/pingcap/tidb/pull/20452](https://github.com/pingcap/tidb/pull/20452)

## Diagnostic Steps

Null

## Resolution

SQL 兼容性，无需修复。

> 注意
>
> v4.0 及以后版本升级到 v5.0 版本需要注意相关的业务逻辑是否存在风险，可能需要业务修改 update 逻辑。

## Workaround

## Affect Version(optional)

v5.0.0