【发布时间】:2012-07-01 06:37:04
【问题描述】:
我需要执行以下操作,但我正在努力使用语法:
我有一个名为“mytable”的表和一个名为“mycolumn”(字符串)的列。
在 mycolumn 中,该值是一个构造值 - 例如,其中一个值是:'first:10:second:18:third:31'。 mycolumn 中的值都使用相同的模式,只是 ids/numbers 不同。
我需要将 18 的值(在这种特殊情况下)更改为另一个表键的值。此列值的最终结果应该是 'first:10:second:22:third:31',因为我将 18 替换为 22。我使用 18 作为查找值从另一个表中获得了 22。
所以理想情况下我会拥有以下内容:
UPDATE mytable
SET mycolumn = [some regex function to find the number between 'second:' and ":third" -
let's call that oldkey - and replace it with other id from another table -
(select otherid from tableb where id = oldkey)].
我知道 mysql 有一个 REPLACE 功能,但这还不够。
【问题讨论】:
-
为什么将数据存储在这些分隔字符串中?你不能把它们分成各自的列吗?
-
是的,我同意,但我正在处理数据库中的一些遗留问题。