简体   繁体   English

8.0 之前的 MySQL regex_replace 功能是否有任何替代方案,不需要 UDF?

[英]Are there any alternatives for MySQL's regex_replace feature pre-8.0 that doesn't require UDFs?

I'm attempting to search records by passing in a squashed version of a column - If I pass in "Martin" I would expect the matching "M-@art*in" would be returned.我正在尝试通过传入列的压缩版本来搜索记录 - 如果我传入“Martin”,我希望会返回匹配的“M-@art*in”。 This is something that REGEX_REPLACE seems to handle well in MySQL 8.0+ and I've seen many recommendations for User Defined Functions (UDFs) that can do the same job pre-8.0.这是 REGEX_REPLACE 似乎在 MySQL 8.0+ 中处理得很好,我已经看到了很多关于用户定义函数 (UDF) 的建议,它们可以在 8.0 之前完成相同的工作。 My supervisor doesn't want to rely on UDFs, and so I am wondering if there are any other options?我的主管不想依赖 UDF,所以我想知道是否还有其他选择?

Any help is greatly appreciated.任何帮助是极大的赞赏。

Use something like DBD::mysql , extract the column and update them.使用类似DBD::mysql的东西,提取列并更新它们。

Or, come to think of it, set your collation to UTF8或者,想想看,将排序规则设置为 UTF8

If you have a specific set of characters allowed, you could do eg replace(replace(replace('M-@art*in','-',''),'@',''),'*','')如果您允许使用一组特定的字符,您可以执行例如replace(replace(replace('M-@art*in','-',''),'@',''),'*','')

If not, you can have a somewhat cumbersome query that looks at each character individually and tests if it should be included in a group_concat that reassembles them:如果没有,你可以有一个有点麻烦的查询,分别查看每个字符并测试它是否应该包含在重新组合它们的 group_concat 中:

select id, group_concat(substr(bar,i,substr(bar,i,1) regexp '[a-z]') order by i separator '') clean_bar
from (select 1 i union select 2 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10) i
join foo on i <= length(bar)
group by foo.id

fiddle 小提琴

But you'd probably want to run that on the data and store the squashed version in a separate column if you are trying to search on it.但是,如果您尝试对其进行搜索,您可能希望在数据上运行它并将压缩后的版本存储在单独的列中。

Note that mysql 5.7 will no longer be supported in 14 months;注意 mysql 5.7 将在 14 个月后不再支持; do plan to upgrade.计划升级。

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

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