从mariadb中的混合字符串中提取UUID

时间:2018-10-24 10:12:40

标签: parsing mariadb

我有一个mariadb表,如下所示:

enter image description here

这是行的外观示例

|  4 | test/1ecb5e71-9105-4a0c-8fa1-7fc8d5e970bd/kuva.jpeg                             | {"Records":

Records中的内容已被省略,以使其简短而简单。当我发出像这样的select key_name from minio_images where id=4;的SQL select时,它会向我返回这样的正常输出

+-----------------------------------------------------+
| key_name                                            |
+-----------------------------------------------------+
| test/1ecb5e71-9105-4a0c-8fa1-7fc8d5e970bd/kuva.jpeg |
+-----------------------------------------------------+
1 row in set (0.09 sec)

我的问题是如何使用select,以便它只返回UUID中的key_name而不是整个字符串。例如,1ecb5e71-9105-4a0c-8fa1-7fc8d5e970bd而不是test/1ecb5e71-9105-4a0c-8fa1-7fc8d5e970bd/kuva.jpeg?非常感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

Luckily, I found a similar post @ extract substring from mysql column using regex and I tried a solution suggested there, by issuing a SELECT statement which I guess, returns the value from a column based on the regular expression. In my case, the regular expression to extract UUID is '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}' and the statement was like

SELECT REGEXP_SUBSTR(key_name, '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}')
FROM minio_images;

Which gave me an output, just like I needed

+--------------------------------------------------------------------------------------------------------+
| REGEXP_SUBSTR(key_name, '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}') |
+--------------------------------------------------------------------------------------------------------+                                                                                               


| 1ecb5e71-9105-4a0c-8fa1-7fc8d5e970bd                                                               |
| 1edd460e-b19a-4b16-a460-d433eac60833                                                                   |
| 281a890f-0b8b-4693-9227-fc8c57d6045e                                                                   |
| 37a14ddb-eeda-41f2-a2b6-ec0bad34aaed                                                                   |
| 37d4f3d2-2282-4b9f-8e1e-f8a26570c5b4                                                                       |
| 387da0c1-1caf-4394-a023-92e7eec19b66                                                                   |
| 49a29478-4799-4a8b-8757-42060020fc99                                                                   |
| 9214e1f0-77e3-435a-a329-d1829a973903                                                                   |
| ae67c69a-a2cf-4c21-88ca-bd17e254bc4c                                                                   |
| b6491e64-34a6-4aa3-a54e-200b1cd946fe                                                                   |
| c0f6864b-2ab8-41fa-a1c2-6b974a1895c1                                                                   |
| cfd61927-557e-47d2-aeb9-229ec1aba5b4                                                                   |
| df566110-c2a0-4d9c-8389-fcbaf6c8bb30                                                                   |
+-------------------------------------------------------------------------------------   -------------------+
16 rows in set (0.03 sec)