3

I read the mysql documentation about regexp but looks like the regexp can be used only as WHERE filters to match rows in SELECT query.

Im wondering if is possible to extract the matches of the regexp as columns, for example if in my table i have a SKU textfield with:

+----------------+--------------+---------------+
|sku             | product_name | [other fields]|
+----------------+--------------+---------------+
|ART-2830--107-70| Foo          |               |
|ART-2832--115-6 | Bar          |               |
+----------------+--------------+---------------+

it would be great to fire a select query that matches the regexp (ART-)([0-9]+)(--)([0-9]+)(-)([0-9]+) that gives me as output:

+------+------------+-------+----------+
| type | collection | model | material |
+------+------------+-------+----------+
| ART  | 2830       | 107   | 70       |
| ART  | 2832       | 115   | 6        |
+------+------------+-------+----------+

Please note that type, collection, model and material columns are not present into my table, and are merely the matches of the regexp above.

Is this possible?

p.s: I know that would be a GREAT idea to normalize that table adding a column for each property i need, but this is the structure I have to work with and I can not edit this at the moment.

EDIT Just in case someone need this, I saw that PostgreSql offer this function.

4

2 回答 2

4

一般来说,答案是“不”——至少在没有大量蛮力编码的情况下是这样。但是,你很幸运,因为你可以在 MySQL 中“轻松”地做你想做的事:

select substring_index(sku, '-', 1) as type,
       reverse(substring_index(reverse(substring_index(sku, '-', 2)), '-', 1)) as collection,
       reverse(substring_index(reverse(substring_index(sku, '-', 4)), '-', 1)) as model,
       reverse(substring_index(reverse(substring_index(sku, '-', 5)), '-', 1)) as material
from skus

好吧,也许不是最明显的功能集合。但这是它对模型的作用:

从...开始: ART-2830--107-70

substring_index(sku, '-', 4) --> ART-2830--107

reverse(): _

701--0381-TRA

外层substring_index( . . . 1)

701

最后reverse()

107
于 2013-06-17T13:16:45.357 回答
0

在 select 语句中使用正则表达式实际上取决于 sql 语言。对于 MySql,您可以在手册中阅读更多内容。

手册的这一部分讨论了使用正则表达式选择语句:http ://dev.mysql.com/doc/refman/5.7/en/regexp.html 。该手册页还具有以下链接,指向如何将正则表达式语句用于 where 语句的示例。

http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html

这个例子也在手册中。

To find names containing a “w”, use this query:

mysql> SELECT * FROM pet WHERE name REGEXP 'w';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

您可以根据正则表达式捕获组返回数据吗?

否。 where 语句过滤 SQL 引擎返回的数据。您需要为要返回的每一列数据建立一个条目。正如 Gordon Linoff 的回答一样,每一列都可以由 SQL 处理,以手动从给定列中提取数据。但是这种解决方案远非使用正则表达式来描述 sql 选择语句的理想。

我打算使用正则表达式捕获组来提取数据,那么这需要在您收到数据后完成。

于 2013-06-17T13:16:26.903 回答