0

I have a table with a column containing many urls like this one:

https://myshop.com/lv/buitine-technika-elektronika/virtuves-iranga/virduliai/virdulys-elektrinis-virdulys-forme-fkg-147?id=22031685

I want to extract the id from the URL, but I have no idea how to do this. I could easily do this in Python later using this regex:

\?id\=(\d+)

But I'd like to have as much of data prepared in my query before going to python if that is possible. I know how to use regex in MySQL where clause, but no idea how to use it anywhere else. Is there a way to do this?

ID length can be different and there might be something else after that...

1
  • Some tasks (such as this one) are better off done in the Client. Commented Jul 9, 2019 at 22:45

1 Answer 1

2

If every URL would only ever have a single id query parameter, then we can use SUBSTRING_INDEX:

SELECT SUBSTRING_INDEX(url, '?id=', -1) AS id
FROM yourTable;

Demo

For a more general solution in MySQL 8+, we can use REGEXP_SUBSTRING:

SELECT REGEXP_REPLACE(url, '^.*?.*id=([^&]+).*$', '$1') AS id
FROM yourTable;

Demo

The regex based approach can handle id appearing anywhere in the query string.

Sign up to request clarification or add additional context in comments.

6 Comments

ant there migth be something else after that...
@BenM Pre MySQL 8+ can't really cope with that requirement in general, assuming the id could appear anywhere in the query string. But, MySQL 8+ can using REGEXP_REPLACE.
@TimBiegeleisen I believe that regexp_replace literally makes an edit in a database which I am not allowed to do
@milka1117 Running a SELECT query absolutely does not alter any data in your table, regardless of which function gets called.
@Aurieli I don't think that MySQL 8's regex API supports lookbehinds (though I could be wrong).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.