简体   繁体   English

在mysql中使用正则表达式替换表中的列值

[英]Replace a column value in a table using regex in mysql

I need to do the following and I'm struggling with the syntax: 我需要执行以下操作,并且我在语法上苦苦挣扎:

I have a table called 'mytable' and a column called 'mycolumn' (string). 我有一个名为“ mytable”的表和一个名为“ mycolumn”(字符串)的列。

In mycolumn the value is a constructed value - for example, one of the values is: 'first:10:second:18:third:31'. 在mycolumn中,该值为构造值-例如,其中一个值为:“ first:10:second:18:third:31”。 The values in mycolumn are all using the same pattern, just the ids/numbers are different. mycolumn中的值都使用相同的模式,只是id /数字不同。

I need to change the value of 18 (in this particular case) to a value from another tables key. 我需要将值18(在这种情况下)更改为另一个表键的值。 The end result for this column value should be 'first:10:second:22:third:31' because I replaced 18 with 22. I got the 22 from another table using the 18 as a lookup value. 该列值的最终结果应为“ first:10:second:22:third:31”,因为我用22替换了18。我从另一个表中获取了22,并使用18作为查找值。

So ideally I would have the following: 因此,理想情况下,我将具有以下内容:

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)].

I know the mysql has a REPLACE function but that doesn't get me far enough. 我知道mysql有一个REPLACE函数,但是那还不够。

You can create your own function. 您可以创建自己的函数。 I am scared of REGEX so I use SUBSTRING and SUBSTRING_INDEX. 我担心REGEX,所以我使用SUBSTRING和SUBSTRING_INDEX。

CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
   LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
   delim, '');

SPLIT_STRING('first:10:second:18:third:31', ':', 4)

returns 18 返回18

Based on this answer: 基于此答案:

Equivalent of explode() to work with strings in MySQL 等效于explode()以在MySQL中使用字符串

You want something like this, where it matches the group: 您想要这样的东西,它与组匹配:

WHERE REGEXP 'second:([0-9]*):third'

However, MySQL doesn't have a regex replace function, so you would have to use a user-defined function: 但是,MySQL没有正则表达式替换功能,因此您必须使用用户定义的功能:

REGEXP_REPLACE?(text, pattern, replace [,position [,occurence [,return_end [,mode]]])

User-defined function is available here: 用户定义的功能在此处可用:

http://www.mysqludf.org/lib_mysqludf_preg/ http://www.mysqludf.org/lib_mysqludf_preg/

The problem with MySQL is it's REGEX flavor is very limited and does not support back references or regex replace, which pretty much makes it impossible to replace the value like you want to with MySQL alone. MySQL的问题在于它的REGEX风格非常有限,并且不支持反向引用或regex替换,这几乎使得无法像单独使用MySQL那样替换所需的值。

I know it means taking a speed hit, but you may want to consider selecting the row you want with by it's id or however you select it, modify the value with PHP or whatever language you have interfacing with MySQL and put it back in with an UPDATE query. 我知道这意味着加快速度,但是您可能要考虑通过其ID选择想要的行,或者选择该行,然后使用PHP或与MySQL接口的任何语言修改值,然后使用UPDATE查询。

Generally speaking, REGEX in programming languages is much more powerful. 一般来说,编程语言中的REGEX功能要强大得多。

If you keep those queries slim and quick, you shouldn't take too big of a speed hit (probably negligible). 如果您将这些查询保持精简和快速,那么您不应受到太大的影响(可能忽略不计)。

Also, here is documentation on what MySQL's REGEX CAN do. 另外,这里是有关MySQL的REGEX可以做什么的文档。 http://dev.mysql.com/doc/refman/5.1/en/regexp.html http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Cheers 干杯

EDIT: 编辑:

To be honest, eggyal's comment makes a whole lot more sense for your situation (simple int values). 坦白地说,eggyal的注释对于您的情况(简单的int值)更加有意义。 Just break them up into columns there's no reason to access them like that at all imo. 只需将它们分成几列,就没有理由像在所有imo上那样访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM