mysql 中的 REGEXP_SUBSTR 相当于什么?
我想从表的字符串列中提取一个单词。
description
===========================
abc order_id: 2 xxxx yyy aa
mmm order_id: 3 nn kk yw
预期结果集
order_id
===========================
2
3
表最多有 100 行,文本长度约为 256 个字符,并且列始终有一个 order_id
存在。所以性能不是问题。
在 Oracle 中,我可以使用 REGEXP_SUBSTR 来解决这个问题。我该如何在 MySQL 中解决这个问题?
编辑1
我正在使用 LOCATE 和 SUBSTR 来解决问题。代码很丑陋。写完代码十分钟后,我就咒骂写出如此丑陋代码的家伙。
我在 MySQL 文档中没有找到 REGEXP_SUBSTR 函数。但我希望它存在..
回答:为什么不能优化表?为什么数据以如此愚蠢的方式存储?
我给出的例子只是表示我正在尝试解决的问题。在实际场景中,我使用基于数据库的第三方排队软件来执行异步任务。队列将 Ruby 对象序列化为文本。我无法控制表结构或数据格式。队列中的任务可以重复出现。在我们的测试设置中,一些重复任务由于数据陈旧而失败。我必须删除这些任务以防止出现错误。此类错误并不常见,因此我不想维护规范化的影子表。
I want to extract a word from a string column of a table.
description
===========================
abc order_id: 2 xxxx yyy aa
mmm order_id: 3 nn kk yw
Expected result set
order_id
===========================
2
3
Table will at most have 100 rows, text length is ~256 char and column always has one order_id
present. So performance is not an issue.
In Oracle, I can use REGEXP_SUBSTR
for this problem. How would I solve this in MySQL?
Edit 1
I am using LOCATE and SUBSTR to solve the problem. The code is ugly. Ten minutes after writing the code, I am cursing the guy who wrote such an ugly code.
I didn't find the REGEXP_SUBSTR function in MySQL docs. But I am hoping that it exists..
Answer to : Why cant the table be optimized? Why is the data stored in such a dumb fashion?
The example I gave just denotes the problem I am trying to solve. In real scenario, I am using a DB based 3rd party queuing software for executing asynchronous tasks. The queue serializes the Ruby object as text. I have no control over the table structure OR the data format. The tasks in the queue can be recurring. In our test setup, some of the recurring tasks are failing because of stale data. I have to delete these tasks to prevent the error. Such errors are not common, hence I don't want to maintain a normalized shadow table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,从 MySQL 8.0 开始支持它。 正则表达式:
Yes, starting from MySQL 8.0 it is supported. Regular Expressions:
正如 Konerak 所说,MySql 中没有 REGEXP_SUBSTR 的等效项。您可以使用 SUBSTRING 逻辑执行您需要的操作,但它很难看:
编辑:您可以尝试这个用户定义函数提供对 MySql 中 perl 正则表达式的访问
Like Konerak said, there is no equivalent of REGEXP_SUBSTR in MySql. You could do what you need using SUBSTRING logic, but it is ugly :
Edit: You could try this user defined function providing access to perl regex in MySql
或者你可以这样做并避免丑陋:
or you can do this and save yourself the ugliness :
没有 MySQL 的等效项。 MySQL REGEXP 可用于匹配字符串,但不能用于转换它们。
您可以尝试使用存储过程和大量 REPLACE/SUBSTRING 逻辑,或者使用您的编程语言进行操作 - 这应该是最简单的选择。
但您确定您的数据格式选择正确吗?如果您需要 order_id,那么将其存储在不同的列中是否有意义,以便您可以放置索引、使用联接等?
There is no MySQL equivalent. The MySQL REGEXP can be used for matching strings, but not for transforming them.
You can either try to work with stored procedures and a lot of REPLACE/SUBSTRING logic, or do it in your programming language - which should be the easiest option.
But are you sure your data format is well chosen? If you need the order_id, wouldn't it make sense to store it in a different column, so you can put indexes, use joins and the likes?