MySQL: Check if first character is _not_ A-Z
I have to create an SQL Query to get all rows starting with a specific character, except if the parameter passed to the (PHP) function is 0, in that case it should get every row that does not start with A - Z (l开发者_运维知识库ike #0-9.,$ etc).
What is the easiest and fastest way to get those rows?
DB: MySQL 5.1
Column: titleSELECT *
FROM mytable
WHERE title NOT RLIKE '^[A-Z]'
SELECT *
FROM myTable
WHERE theColumn RLIKE '^[^A-Z]'
Explanation:
With RLIKE predicate, a bracketed pattern stands for any one character listed within the bracket (allowing for the use of a dash to indicate ranges, as here, A-Z is "A through Z").
Note the two ^ characters; they have a very distinct meaning:
The first one stands for "beginning of string"
The other ^ character, within the brackets indicates that the pattern should match any character which is not listed thereafter in the bracketed list.
See the mySQL documentation on pattern matching for more details.
The could also be alternatively written as follow
...
WHERE theColumn NOT RLIKE '^[A-Z]'
A word of caution: Independently of the bracketed syntax, the patterns shown above both use a "front end wildcard", which can only be processed by way of a table/index scan (or for the least a partial scan) and this can be relatively inefficient with bigger tables.
精彩评论