【发布时间】:2013-06-17 12:26:21
【问题描述】:
我阅读了mysql documentation about regexp,但看起来正则表达式只能用作WHERE 过滤器以匹配SELECT 查询中的行。
我想知道是否可以将正则表达式的匹配项提取为列,例如,如果在我的表中我有一个 SKU 文本字段:
+----------------+--------------+---------------+
|sku | product_name | [other fields]|
+----------------+--------------+---------------+
|ART-2830--107-70| Foo | |
|ART-2832--115-6 | Bar | |
+----------------+--------------+---------------+
最好触发一个匹配正则表达式 (ART-)([0-9]+)(--)([0-9]+)(-)([0-9]+) 的选择查询,它给我作为输出:
+------+------------+-------+----------+
| type | collection | model | material |
+------+------------+-------+----------+
| ART | 2830 | 107 | 70 |
| ART | 2832 | 115 | 6 |
+------+------------+-------+----------+
请注意,type, collection, model and material 列不存在于我的表中,只是上面正则表达式的匹配项。
这可能吗?
p.s:我知道规范化该表为我需要的每个 property 添加一列是一个好主意,但这是我必须使用的结构,我无法在瞬间。
编辑以防万一有人需要,我看到了PostgreSql offer this function。
【问题讨论】: