mysql 中的 REGEXP_SUBSTR 相当于什么?

发布于 2024-08-30 20:22:06 字数 730 浏览 3 评论 0原文

我想从表的字符串列中提取一个单词。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌逼全场 2024-09-06 20:22:06

“我在 MySQL 文档中没有找到 REGEXP_SUBSTR 函数。但我希望它存在......”

是的,从 MySQL 8.0 开始支持它。 正则表达式

REGEXP_SUBSTR(expr, pat[, pos[,occurrence[, match_type]]])

返回字符串 expr 中与模式 pat 指定的正则表达式相匹配的子字符串,如果没有匹配则返回 NULL。如果expr或pat为NULL,则返回值为NULL。

"I didn't find the REGEXP_SUBSTR function in MySQL docs. But I am hoping that it exists.."

Yes, starting from MySQL 8.0 it is supported. Regular Expressions:

REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]])

Returns the substring of the string expr that matches the regular expression specified by the pattern pat, NULL if there is no match. If expr or pat is NULL, the return value is NULL.

无人问我粥可暖 2024-09-06 20:22:06

正如 Konerak 所说,MySql 中没有 REGEXP_SUBSTR 的等效项。您可以使用 SUBSTRING 逻辑执行您需要的操作,但它很难看:

SELECT
  SUBSTRING(lastPart.end, 1, LOCATE(' ', lastPart.end) - 1) AS orderId
FROM
  (
    SELECT
      SUBSTRING(dataset.description, LOCATE('order_id: ', dataset.description) + LENGTH('order_id: ')) AS end
    FROM
      (
        SELECT 'abc order_id: 2 xxxx yyy aa' AS description
        UNION SELECT 'mmm order_id: 3 nn kk yw' AS description
        UNION SELECT 'mmm order_id: 1523 nn kk yw' AS description
      ) AS dataset
    ) AS lastPart

编辑:您可以尝试这个用户定义函数提供对 MySql 中 perl 正则表达式的访问

SELECT 
  PREG_CAPTURE( '/.*order_id:\s(\d+).*/', dataset.description,1)
FROM
  (
    SELECT 'abc order_id: 2 xxxx yyy aa' AS description
    UNION SELECT 'mmm order_id: 3 nn kk yw' AS description
    UNION SELECT 'mmm order_id: 1523 nn kk yw' AS description
  ) AS dataset

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 :

SELECT
  SUBSTRING(lastPart.end, 1, LOCATE(' ', lastPart.end) - 1) AS orderId
FROM
  (
    SELECT
      SUBSTRING(dataset.description, LOCATE('order_id: ', dataset.description) + LENGTH('order_id: ')) AS end
    FROM
      (
        SELECT 'abc order_id: 2 xxxx yyy aa' AS description
        UNION SELECT 'mmm order_id: 3 nn kk yw' AS description
        UNION SELECT 'mmm order_id: 1523 nn kk yw' AS description
      ) AS dataset
    ) AS lastPart

Edit: You could try this user defined function providing access to perl regex in MySql

SELECT 
  PREG_CAPTURE( '/.*order_id:\s(\d+).*/', dataset.description,1)
FROM
  (
    SELECT 'abc order_id: 2 xxxx yyy aa' AS description
    UNION SELECT 'mmm order_id: 3 nn kk yw' AS description
    UNION SELECT 'mmm order_id: 1523 nn kk yw' AS description
  ) AS dataset
别理我 2024-09-06 20:22:06

或者你可以这样做并避免丑陋:

select SUBSTRING_INDEX(SUBSTRING_INDEX('habc order_id: 2 xxxx yyy aa',' ',3),' ',-1);

or you can do this and save yourself the ugliness :

select SUBSTRING_INDEX(SUBSTRING_INDEX('habc order_id: 2 xxxx yyy aa',' ',3),' ',-1);
安静被遗忘 2024-09-06 20:22:06

没有 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文