panthro
panthro

Reputation: 24061

Using Like with strings

I count rows where title is:

this-is-the-title

And if count is greater than 0 I append the count to the title and insert:

this-is-the-title-1

When inserting again I will need to check again but the where title = 'this is the title' will not catch any titles with appended numbers.

So I try:

LIKE 'this-is-the-title%'

This works but now it would also pick up any titles such as:

this-is-the-title-extra-string

How can I query the table so that I can count the number of instances of 'this-is-the-title' and any subsequent 'this-is-the-title-nth

Upvotes: 2

Views: 51

Answers (1)

MK.
MK.

Reputation: 34527

select * from table
where column REGEXP '^this-is-the-title(-[0-9]+)?$'

Regular expressions are an ok solution for things like that. The question mark says that the trailing group is optional, i.e. it will match w/o it and [0-9]+ means one or more digit. ^ at the beginning means match at the beginning of line, $ means match at the end. Together they ensure the full string match.

Upvotes: 1

Related Questions