Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于其他列的MySQL更新列_Mysql_Sql_Sql Update - Fatal编程技术网

基于其他列的MySQL更新列

基于其他列的MySQL更新列,mysql,sql,sql-update,Mysql,Sql,Sql Update,我有这张桌子: table_a +----+--------+ | id | status | +----+--------+ | 10 | ERROR | +----+--------+ table_b +------------+----------------+ | trading_id | trading_status | +------------+----------------+ | 10 | CLOSED | +------------+---

我有这张桌子:

table_a
+----+--------+
| id | status |
+----+--------+
| 10 | ERROR  |
+----+--------+
table_b
+------------+----------------+
| trading_id | trading_status |
+------------+----------------+
|         10 | CLOSED         |
+------------+----------------+
我还有一张桌子:

table_a
+----+--------+
| id | status |
+----+--------+
| 10 | ERROR  |
+----+--------+
table_b
+------------+----------------+
| trading_id | trading_status |
+------------+----------------+
|         10 | CLOSED         |
+------------+----------------+
如何基于
表b.交易状态
更新表a.状态,使结果如下:

table_a
+----+--------+
| id | status |
+----+--------+
| 10 | CLOSED |
+----+--------+
试试这个:

UPDATE table_a
SET status = COALESCE((SELECT trading_status
                       FROM table_b 
                       WHERE trading_id = table_a.id), table_a.status)
上述查询假设
表b
中最多有一条记录与
表a
中的每条记录相匹配


您可以使用更新连接语法:

UPDATE table_a
JOIN   table_b ON table_a.id = table_b.id
SET    table_a.status = table_b.trading_status