| Name | Description |
|---|---|
<=> |
NULL-safe equal to operator |
= |
Equal operator |
>= |
Greater than or equal operator |
> |
Greater than operator |
IS NOT |
Test a value against a boolean |
IS NULL |
NULL value test |
IS |
Test a value against a boolean |
<= |
Less than or equal operator |
< |
Less than operator |
LIKE |
Simple pattern matching |
!=, <> |
Not equal operator |
NOT LIKE |
Negation of simple pattern matching |
SOUNDS LIKE(v4.1.0) |
Compare sounds |
If a string function is given a binary string as an argument, the resulting string is also a binary string. A number converted to a string is treated as a binary string. This affects only comparisons.
Normally, if any expression in a string comparison is case sensitive, the comparison is performed in case-sensitive fashion.
-
exprLIKEpat[ESCAPE 'escape_char']Pattern matching using SQL simple regular expression comparison. Returns
1(TRUE) or0(FALSE). If eitherexprorpatisNULL, the result isNULL.The pattern need not be a literal string. For example, it can be specified as a string expression or table column.
Per the SQL standard,
LIKEperforms matching on a per-character basis, thus it can produce results different from the=comparison operator:mysql>
SELECT 'ä' LIKE 'ae' COLLATE latin1_german2_ci;+-----------------------------------------+ | 'ä' LIKE 'ae' COLLATE latin1_german2_ci | +-----------------------------------------+ | 0 | +-----------------------------------------+ mysql>SELECT 'ä' = 'ae' COLLATE latin1_german2_ci;+--------------------------------------+ | 'ä' = 'ae' COLLATE latin1_german2_ci | +--------------------------------------+ | 1 | +--------------------------------------+With
LIKEyou can use the following two wildcard characters in the pattern:Character Description %Matches any number of characters, even zero characters _Matches exactly one character mysql>
SELECT 'David!' LIKE 'David_';-> 1 mysql>SELECT 'David!' LIKE '%D%v%';-> 1To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the
ESCAPEcharacter, “\” is assumed.String Description \%Matches one “ %” character\_Matches one “ _” charactermysql>
SELECT 'David!' LIKE 'David\_';-> 0 mysql>SELECT 'David_' LIKE 'David\_';-> 1To specify a different escape character, use the
ESCAPEclause:mysql>
SELECT 'David_' LIKE 'David|_' ESCAPE '|';-> 1The escape sequence should be empty or one character long. As of MySQL 5.0.16, if the
NO_BACKSLASH_ESCAPESSQL mode is enabled, the sequence cannot be empty.The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:
mysql>
SELECT 'abc' LIKE 'ABC';-> 1 mysql>SELECT 'abc' LIKE BINARY 'ABC';-> 0In MySQL,
LIKEis allowed on numeric expressions. (This is an extension to the standard SQLLIKE.)mysql>
SELECT 10 LIKE '1%';-> 1Note
Because MySQL uses C escape syntax in strings (for example, “
\n” to represent a newline character), you must double any “\” that you use inLIKEstrings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against. (Exception: At the end of the pattern string, backslash can be specified as “\\”. At the end of the string, backslash stands for itself because there is nothing following to escape.) -
exprNOT LIKEpat[ESCAPE 'escape_char']This is the same as
NOT (.exprLIKEpat[ESCAPE 'escape_char'])Note
Aggregate queries involving
NOT LIKEcomparisons with columns containingNULLmay yield unexpected results. For example, consider the following table and data:CREATE TABLE foo (bar VARCHAR(10)); INSERT INTO foo VALUES (NULL), (NULL);
The query
SELECT COUNT(*) FROM foo WHERE bar LIKE '%baz%';returns0. You might assume thatSELECT COUNT(*) FROM foo WHERE bar NOT LIKE '%baz%';would return2. However, this is not the case: The second query returns0. This is becauseNULL NOT LIKEalways returnsexprNULL, regardless of the value ofexpr. The same is true for aggregate queries involvingNULLand comparisons usingNOT RLIKEorNOT REGEXP. In such cases, you must test explicitly forNOT NULLusingOR(and notAND), as shown here:SELECT COUNT(*) FROM foo WHERE bar NOT LIKE '%baz%' OR bar IS NULL;
-
,exprNOT REGEXPpatexprNOT RLIKEpatThis is the same as
NOT (.exprREGEXPpat) -
,exprREGEXPpatexprRLIKEpatPerforms a pattern match of a string expression
expragainst a patternpat. The pattern can be an extended regular expression. The syntax for regular expressions is discussed in Section 11.4.2, “Regular Expressions”. Returns1ifexprmatchespat; otherwise it returns0. If eitherexprorpatisNULL, the result isNULL.RLIKEis a synonym forREGEXP, provided formSQLcompatibility.The pattern need not be a literal string. For example, it can be specified as a string expression or table column.
Note
Because MySQL uses the C escape syntax in strings (for example, “
\n” to represent the newline character), you must double any “\” that you use in yourREGEXPstrings.REGEXPis not case sensitive, except when used with binary strings.mysql>
SELECT 'Monty!' REGEXP 'm%y%%';-> 0 mysql>SELECT 'Monty!' REGEXP '.*';-> 1 mysql>SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';-> 1 mysql>SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';-> 1 0 mysql>SELECT 'a' REGEXP '^[a-d]';-> 1REGEXPandRLIKEuse the current character set when deciding the type of a character. The default islatin1(cp1252 West European).Warning
The
REGEXPandRLIKEoperators work in byte-wise fashion, so they are not multi-byte safe and may produce unexpected results with multi-byte character sets. In addition, these operators compare characters by their byte values and accented characters may not compare as equal even if a given collation treats them as equal. -
STRCMP(expr1,expr2)STRCMP()returns0if the strings are the same,-1if the first argument is smaller than the second according to the current sort order, and1otherwise.mysql>
SELECT STRCMP('text', 'text2');-> -1 mysql>SELECT STRCMP('text2', 'text');-> 1 mysql>SELECT STRCMP('text', 'text');-> 0STRCMP()uses the current character set when performing comparisons. This makes the default comparison behavior case insensitive unless one or both of the operands are binary strings.
Filed under: MySql