With MySQL 5.7, I have the following query:
SELECT `slug` FROM `unittest`.`test_item`
WHERE `slug` REGEXP '^some-slug[0-9]*$'
ORDER BY `slug` DESC LIMIT 1;
That works just fine for the items with the slugs some-slug1
to some-slug9
, but once I have a slug like some-slug10
, that one won’t be found. Instead, the result is some-slug9
all the time.
I’ve set up an example at regex101.com, where it’s seems to work.
What am I missing here regarding MySQL?
2
Answers
Another option:
see: DBFIDDLE
EDIT: As a bonus (just to learn)
Why is this also order correctly?
see: DBFIDDLE
HINT: Just add
CAST(REGEXP_REPLACE(
slug,'[a-z]','') as UNSIGNED)
to your select, and you will see why.If you change your query to:
SUBSTRING(`slug`, 10)
will return numeric component as a string and then+0
causes an implicit conversion to a numeric value (DOUBLE according to Workbench).You could also:
to explicitly
CAST()
the numeric component to anUNSIGNED BIGINT
.Here’s a db<>fiddle.