mysql - 使用基于 id 的新列值更新整个列
问题描述
使用 MySQL,我当前的表看起来像
表 A:
id_u | date | other | calculation
----------------------------------
1 | 2019-09-09 | "" | 1
2 | 2019-09-09 | "" | 1
4 | 2019-09-09 | "" | 2
5 | 2019-09-09 | "" | 4
我有一个返回的查询(SELECT)
询问:
id_u | calculation
-------------------
2 | 12
4 | 16
更新后想要的结果:
id_u | date | other | calculation
----------------------------------
1 | 2019-09-09 | "" | 1
2 | 2019-09-09 | "" | 12
4 | 2019-09-09 | "" | 16
5 | 2019-09-09 | "" | 4
如何更新calculation
每个 ids 的列?
解决方案
在MySQL
中,您可以使用语句JOIN
中的子句UPDATE
来执行跨表更新:
update tableA a inner join (
select id_u, calculation
from <some table[s][joins]>
) b on a.id_u = b.id_u
set a.calculation = b.calculation;
其中,在这种情况下,使用的是在查询子句derived table
范围之后生成表的表达式。INNER JOIN
推荐阅读
- regex - 带有特殊字符和双引号的字符串的 Perl 正则表达式
- flutter - 首次运行消费者
抛出错误 - c# - .NET/Angular 应用程序中的 JWT 令牌身份验证无效
- java - 如何使用java从字符串中删除非英文字符?
- asp.net-core - 如何在asp.net core razor中将选择的单选按钮值发送到模态
- racket - 在 Racket 中动态加载模块
- java - 如何从java中的contect-type“application/pdf”中提取文本内容
- python - 无法转换参数
- docusignapi - 使用 PHP 中的标记字段通过电子邮件发送 DocuSign PDF
- c++ - Nlohmann Json“array”和“array_t”之间的区别