0

I have a column following this pattern:

XXXXX-and_some_text

where XXXXX can be a number from 3 to 5 digits followed by a dash. A regex like that:

\d{3,5}-

will find same rows as LIKE "XXX%"

I want to find rows which match exactly, f.e.

looking for 903 should find 903-Text and not 9030-Other_Text or 90344-Another-Text.

I could filter the rows afterwards in my php code but it would be much nicer to accomplish that in one single sql statement.

Any ideas?

2
  • 1
    LIKE '903-%' will work. Commented Jul 17, 2019 at 13:27
  • MySQL has regexes since 8.0 Commented Jul 17, 2019 at 13:41

1 Answer 1

1

Here are a few methods:

where col like 'XXX-%'
where col regexp '^XXX-'
where substring_index(col, '-', 1) = 'XXX'

The advantage of the first method is that it might use an index on the column, if available.

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.