我的问题很简单,但我无法理解。我需要用表2中的秘密数据填充秘密列,但有几个条件:
表1:
+----+-------+------------+---------+--------+
| id | color | date | quantiy | secret |
+----+-------+------------+---------+--------+
| 1 | blue | 2017-10-29 | 7 | |
+----+-------+------------+---------+--------+
| 2 | blue | 2017-10-29 | 13 | |
+----+-------+------------+---------+--------+
| 3 | blue | 2017-10-29 | 13 | |
+----+-------+------------+---------+--------+
| 4 | blue | 2017-10-30 | 5 | |
+----+-------+------------+---------+--------+
| 5 | red | 2017-10-29 | 10 | |
+----+-------+------------+---------+--------+
| 6 | red | 2017-10-29 | 8 | |
+----+-------+------------+---------+--------+
表2:
+----+-------+------------+---------+------------------+--------+
| id | color | date | quantiy | quantity_balance | secret |
+----+-------+------------+---------+------------------+--------+
| 11 | blue | 2017-10-29 | 7 | 120 | abc |
+----+-------+------------+---------+------------------+--------+
| 12 | blue | 2017-10-29 | 13 | 113 | def |
+----+-------+------------+---------+------------------+--------+
| 13 | blue | 2017-10-29 | 13 | 100 | ghi |
+----+-------+------------+---------+------------------+--------+
| 14 | blue | 2017-10-30 | 5 | 87 | jkl |
+----+-------+------------+---------+------------------+--------+
| 15 | red | 2017-10-29 | 10 | 201 | mno |
+----+-------+------------+---------+------------------+--------+
| 16 | red | 2017-10-29 | 8 | 191 | pqr |
+----+-------+------------+---------+------------------+--------+
答案 0 :(得分:1)
由于您没有选择要更新的行的独特方式,因此无法通过简单的连接执行此操作。
我将用一种需要MySQL 8.0中的窗口函数的方法来回答这个问题。
+------------------------------------------------+
| _sql |
+------------------------------------------------+
| UPDATE table1 SET secret = 'abc' WHERE id = 1; |
| UPDATE table1 SET secret = 'def' WHERE id = 2; |
| UPDATE table1 SET secret = 'ghi' WHERE id = 3; |
| UPDATE table1 SET secret = 'jkl' WHERE id = 4; |
| UPDATE table1 SET secret = 'pqr' WHERE id = 6; |
| UPDATE table1 SET secret = 'mno' WHERE id = 5; |
+------------------------------------------------+
输出是一系列UPDATE语句:
{{1}}
我们不能直接执行UPDATE,因为CTE不可更新。
答案 1 :(得分:0)
可以在不影响原始表的情况下创建视图,并且可以合并它们并且可以通过Quantity
匹配列
视图将是您的数据库中存在的虚拟表,您可以在系统中的任何位置使用它