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]%'; 

      

+3


source to share


2 answers


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/

+3


source


Use REGEXP

notLIKE



SELECT author 
FROM lyrics 
WHERE author REGEXP '[^[:alnum:]]'

      

+2


source







All Articles