Rechercher dans le manuel MySQL
12.5.1 String Comparison Functions
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.
expr
LIKEpat
[ESCAPE 'escape_char
']Pattern matching using an SQL pattern. Returns
1
(TRUE
) or0
(FALSE
). If eitherexpr
orpat
isNULL
, 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,
LIKE
performs matching on a per-character basis, thus it can produce results different from the=
comparison operator:- +-----------------------------------------+
- +-----------------------------------------+
- | 0 |
- +-----------------------------------------+
- +--------------------------------------+
- +--------------------------------------+
- | 1 |
- +--------------------------------------+
In particular, trailing spaces are significant, which is not true for
CHAR
orVARCHAR
comparisons performed with the=
operator:- +------------+---------------+
- +------------+---------------+
- | 1 | 0 |
- +------------+---------------+
With
LIKE
you can use the following two wildcard characters in the pattern:%
matches any number of characters, even zero characters._
matches exactly one character.
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.\%
matches one%
character.\_
matches one_
character.
To specify a different escape character, use the
ESCAPE
clause:The escape sequence should be empty or one character long. The expression must evaluate as a constant at execution time. 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 case-sensitive (uses a case-sensitive collation or is a binary string):
- -> 1
- -> 0
- -> 0
- -> 0
As an extension to standard SQL, MySQL permits
LIKE
on numeric expressions.NoteBecause MySQL uses C escape syntax in strings (for example,
\n
to represent a newline character), you must double any\
that you use inLIKE
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.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. Suppose that a table contains the following values:- +--------------+
- | filename |
- +--------------+
- | C: |
- | C:\ |
- | C:\Programs |
- | C:\Programs\ |
- +--------------+
To test for values that end with backslash, you can match the values using either of the following patterns:
- +--------------+---------------------+
- +--------------+---------------------+
- | C: | 0 |
- | C:\ | 1 |
- | C:\Programs | 0 |
- | C:\Programs\ | 1 |
- +--------------+---------------------+
- +--------------+-----------------------+
- +--------------+-----------------------+
- | C: | 0 |
- | C:\ | 1 |
- | C:\Programs | 0 |
- | C:\Programs\ | 1 |
- +--------------+-----------------------+
expr
NOT LIKEpat
[ESCAPE 'escape_char
']This is the same as
NOT (
.expr
LIKEpat
[ESCAPE 'escape_char
'])NoteAggregate queries involving
NOT LIKE
comparisons with columns containingNULL
may yield unexpected results. For example, consider the following table and data: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 LIKE
always returnsexpr
NULL
, regardless of the value ofexpr
. The same is true for aggregate queries involvingNULL
and comparisons usingNOT RLIKE
orNOT REGEXP
. In such cases, you must test explicitly forNOT NULL
usingOR
(and notAND
), as shown here:STRCMP()
returns0
if the strings are the same,-1
if the first argument is smaller than the second according to the current sort order, and1
otherwise.- -> -1
- -> 1
- -> 0
STRCMP()
performs the comparison using the collation of the arguments.- +------------------+------------------+
- +------------------+------------------+
- | 0 | -1 |
- +------------------+------------------+
If the collations are incompatible, one of the arguments must be converted to be compatible with the other. See Section 10.8.4, “Collation Coercibility in Expressions”.
- -->
- ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT)
- +---------------------------------------------+
- +---------------------------------------------+
- | 0 |
- +---------------------------------------------+
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-string-comparison-functions.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.