简体   繁体   中英

extract multiple matches from varchar or text column as rows from MariaDB

I have a string (varchar ) column in my DB and I would like to use SQL to so some simple group matching and extract the matches into rows. Is there a way to accomplish this in plain SQL in mariaDB without a stored procedure or custom function?

Example:

 my_string ="this is a test string with x12345 and y1264 ...";

I am looking for something like this to extract all numbers starting with x or y into rows.

SELECT REGEXP_SUBSTR("[xy][0-9]+") from my_string;

Expected result:

x12345
y1264

The reason I need the rows is that this will be part of a bigger query where I join these rows on a key in another table.

My above query only returns 1 row, the 1st result x12345

Is there a way to obtain all matches?

You may use JSON_TABLE to extract all words in your string, then check if these words start with x or y and are followed by numbers.

You can simply convert your string to a JSON format by enclosing it with square brackets, enclosing each word with double quotations, and replacing every single space with a comma. So you can use this code to do that CONCAT('["', REPLACE(myTxt, ' ', '","'), '"]') .

Then you can use the JSON_TABLE as the following:

create table tbl(id int, myTxt TEXT);
insert into tbl values
  (1, 'this is a test string with x12345 and y1264'), 
  (2, 'this is a test xtest ytest string with x11111 and y11111 and x22222 and y22222');


SELECT id,
       ROW_NUMBER() OVER (PARTITION BY id ORDER BY W.rowid) num_order, 
       W.xy_val
FROM tbl
CROSS JOIN JSON_TABLE
  (
    CONCAT('["', REPLACE(myTxt, ' ', '","'), '"]'), 
    '$[*]' 
    COLUMNS 
      (
       rowid FOR ORDINALITY, 
       xy_val VARCHAR(32) PATH '$'
      )
  ) W
WHERE  W.xy_val REGEXP '[x][0-9]' OR W.xy_val REGEXP '[y][0-9]'

See demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM