gpt4 book ai didi

mysql - 如何替换mysql中的子字符串,其中字符串基于其他表列值

转载 作者:行者123 更新时间:2023-11-30 00:13:49 25 4
gpt4 key购买 nike

我有两个 mysql 表

Component
+----+-------------------------+--------+
| OldComponentId | NewComponentId |
+----+-------------------------+--------+
| 15 | 85 |
| 16 | 86 |
| 17 | 87 |
+----+-------------------------+--------+

Formulae
+----+-------------------------+--------+
| id | formula_string |
+----+-------------------------+--------+
| 1 | A+15-16+17 |
| 2 | 16+15-17 |
+----+-------------------------+--------+

I want to replace value of formula_string on the basis of NewComponentId as


Formulae
+----+-------------------------+--------+
| id | formula_string |
+----+-------------------------+--------+
| 1 | A+85-86+87 |
| 2 | 86+85-87 |
+----+-------------------------+--------+

我尝试过以下 mysql 查询,但它不起作用

更新公式 fr,组件补偿集 Formula_string=REPLACE(fr.formula_string,comp.OldComponentId,comp.NewComponentId)。

请提出解决方案

谢谢。

最佳答案

没有简单的方法可以做到这一点。正如您在更新语句中观察到的那样,替换内容不会嵌套。他们一次只更换一个。

您可以做的一件事是:

update Formulae fr cross join
Component comp
set formula_string = REPLACE(fr.formula_string, comp.OldComponentId, comp.NewComponentId)
where formula_string like concat('%', comp.OldComponentId, '%')

然后继续运行,直到 row_count() 返回 0

请注意,您的结构可能会导致无限循环(如果 A --> B 且 B --> A)。您还存在“困惑”的问题,因此 10 将被替换为 100。这表明您的整体数据结构可能不正确。也许您应该将公式分解为单独的部分。如果它们只是数字以及 +-,您可以拥有一个包含每个组件的值和符号的联结表。那么你的查询就会容易得多。

关于mysql - 如何替换mysql中的子字符串,其中字符串基于其他表列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com