I’ve been trying (without success) to construct a MYSQL query which will select a group of records with a “title” field starting with a single alphabetical character but ignoring the first word if it’s “The”, “An” or “A”. I’ve found plenty of examples that do this for the ORDER BY part of the query, but it’s the initial WHERE part that I need to do it for, as the order is irrelevant if the correct records haven’t been found. Using
WHERE title LIKE "R%"
will just give me titles that have this as the very first letter (e.g. “Robin Hood”) but won’t match “The Red House”. I think I need some kind of REGEX, but I can’t seem to get it to work.
So for example, given the following movie titles,
Road House
The Return of the King
Mamma Mia
Argo
Titanic
A River Runs Through it
Selecting movie titles that start with “R” would return the following:
The Return of the King
A River Runs Through it
Roadhouse
(other fields omitted)
The easiest way is to programmatically expand the query to something like
This should perform better than a REGEX, as it will be able to use an index, which a REGEX never will.
BTW: The canonical way to do this, is to store the article in a seperate field.