Php mysql first letter query! = [Az]

SELECT * FROM dbname WHERE text = 'a%' (this can get all the `text` fields which begin with the letter "a".) 

But how to get the fields where the first letter! = [Az]?

eg:

 \"hello\" // this first letter begin as \ not range in azいけ // also first letter not begin range in az ... may also have a white space as the first character. 

So how can you use mysql php query to get all these results when the first letter !=[az] ?

+6
source share
4 answers

Try the following:

 SELECT * FROM dbname WHERE text REGEXP '^[^a-zA-Z]'; 

That is, if you want it to be case insensitive (in upper or lower case). If you want to allow capital letters AZ just use:

 SELECT * FROM dbname WHERE text REGEXP '^[^az]'; 

Essentially, the regular expression says it matches any line that doesn't have the letters az at the beginning of the line.

+8
source

Use REGEXP / RLIKE :

 SELECT * FROM dbname WHERE text REGEXP '^[^az]' 
+2
source
 SELECT * FROM dbname WHERE text NOT REGEXP '^[az]'; 
+1
source

A solution other than REGEXP is to use BETWEEN :

 SELECT * FROM dbname WHERE LEFT(text, 1) NOT BETWEEN 'a' AND 'Z'; 

This should be faster than using REGEXP, since MySQL can use indexes for BETWEEN queries, while it cannot respond to REGEXP queries.

However, I do not have available tests, do not try to test.

+1
source

Source: https://habr.com/ru/post/896052/


All Articles