expr
LIKE
pat
[ESCAPE
'escape_char
']
Pattern matching using SQL simple regular expression
comparison. Returns 1
(TRUE
) or 0
(FALSE
). If either
expr
or
pat
is NULL
,
the result is NULL
.
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, LIKE
performs
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 LIKE
you can use the following two
wildcard characters in the pattern:
mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1
To test for literal instances of a wildcard character,
precede it by the escape character. If you do not specify
the ESCAPE
character,
‘\
’ is assumed.
mysql> SELECT 'David!' LIKE 'David\_';
-> 0
mysql> SELECT 'David_' LIKE 'David\_';
-> 1
To specify a different escape character, use the
ESCAPE
clause:
mysql> SELECT 'David_' LIKE 'David|_' ESCAPE '|';
-> 1
The escape sequence should be empty or one character long.
As of MySQL 5.1.2, if the
NO_BACKSLASH_ESCAPES
SQL 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';
-> 0
In MySQL, LIKE
is allowed on numeric
expressions. (This is an extension to the standard SQL
LIKE
.)
mysql> SELECT 10 LIKE '1%';
-> 1
Note: Because MySQL uses C
escape syntax in strings (for example,
‘\n
’ to represent a newline
character), you must double any
‘\
’ that you use in
LIKE
strings. 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.
expr
REGEXP
pat
expr
RLIKE
pat
Performs a pattern match of a string expression
expr
against a pattern
pat
. The pattern can be an
extended regular expression. The syntax for regular
expressions is discussed in Appendix G, Regular Expressions.
Returns 1
if
expr
matches
pat
; otherwise it returns
0
. If either
expr
or
pat
is NULL
,
the result is NULL
.
RLIKE
is a synonym for
REGEXP
, provided for
mSQL
compatibility.
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 your
REGEXP
strings.
REGEXP
is 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]';
-> 1
REGEXP
and RLIKE
use
the current character set when deciding the type of a
character. The default is latin1
(cp1252
West European). Warning:
These operators are not multi-byte safe.