Search non all numeric filenames in SQL with regex and replace result with value from column

Adrian :

I don't know if it's possible, but I am looking for a way in a Mysql database:

  • search for items, where model = 'YES-OK' and image file name is not all numeric (in the example: row id 5) For this I found out that Regex \/my\/dir-ectory\/\b.*[a-zA-Z]+.*\b.jpg should work.

enter image description here

  • Rows that are matched: replace image filename with SKU without comma.

Example database input:

+----+--------+--------+-----------------------------+
| id |  sku   | model  |            image            |
+----+--------+--------+-----------------------------+
|  1 | 123,45 | YES-OK | /my/dir-ectory/abc4.jpg     |
|  2 | 123,46 | YES-OK | /my/dir-ectory/4abc.jpg     |
|  3 | 123,47 | YES-OK | /my/dir-ectory/abcd.jpg     |
|  4 | 123,48 | YES-OK | /my/dir-ectory/3a-baaac.jpg |
|  5 | 123,49 | YES-OK | /my/dir-ectory/12349.jpg    |
+----+--------+--------+-----------------------------+

Example output:

+----+--------+--------+-----------------------------+
| id |  sku   | model  |            image            |
+----+--------+--------+-----------------------------+
|  1 | 123,45 | YES-OK | /my/dir-ectory/12345.jpg    |
|  2 | 123,46 | YES-OK | /my/dir-ectory/12346.jpg    |
|  3 | 123,47 | YES-OK | /my/dir-ectory/12347.jpg    |
|  4 | 123,48 | YES-OK | /my/dir-ectory/12348.jpg    |
|  5 | 123,49 | YES-OK | /my/dir-ectory/12349.jpg    |
+----+--------+--------+-----------------------------+

Is this possible in pure SQL?

Gordon Linoff :

Assuming your image path has the same format in all rows:

select id, sku, model,
       concat(substring_index(image, '/', 3), '/', replace(sku, ',', ''), '.jpg')
from t
where model = 'YES-OK';

And then you can add the regular expression that you have found for the numbers. I find it a little hard to follow.

You can easily incorporate the same logic into an update if you want an update.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=401774&siteId=1