0

Is it possible to make a SQL query by matching a pattern? I know SQL allows for wildcards but I dont think they fit my use case.

Suppose I have a table that contains the following record (represented here in JSON):

Table Name: url_stuff

{
    id: 2
    path: \/user\/(.*)\/
    value: "I am the user path"
}

Then suppose I had the following string representing a URL path:

/user/gandalfthewhite

I would like to make a query that returns this record.

SELECT * FROM url_stuff WHERE path LIKE '/user/gandalfthewhite'

Obviously this wont work, but perhaps there is some other way to use SQL such that /user/gandalfthewhite matches \/user\/(.*)\/ as it would with regex and return the above record.

One solution is obviously to grab all records from the database and search via regex after the fact, but this would not be scaleable for a large number of records. I would ideally be able to grab all matching records with a query directly.

1 Answer 1

1

If I understand correctly, you can just use regexp:

SELECT *
FROM url_stuff
WHERE '/user/gandalfthewhite' REGEXP url
Sign up to request clarification or add additional context in comments.

Comments

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.