Select everything where [first letter starts with a number or special characters]
I want to search the database for a title that starts with a letter, number, or special characters.
For writing well, I use this:
SELECT author FROM lyrics WHERE author LIKE 'B%';
But for number and special characters, I need your help
SELECT author FROM lyrics WHERE author LIKE '[0-9]%';
source to share
The operator LIKE
won't work the way you want here, it accepts static values ββand wildcards ( %
or _
). You have to use regex with mysql REGEXP
. Something like:
SELECT author FROM lyrics WHERE author regexp '^[0-9B]'
All authors starting with B
or should be found . ^
is a leading anchor, meaning the line must start there. []
builds character class, which is a list or a range of characters, when used -
, 0-9
- is any number.
It's not clear what your "special characters" are, but you might consider a negative character class, that is, a list of invalid characters. For example.
SELECT author FROM lyrics WHERE author regexp '^[^.!?]'
will list all authors who did not start with .
, !
or ?
.
Regex demos: https://regex101.com/r/qjjmNq/1/ , https://regex101.com/r/qjjmNq/2/
source to share