Rechercher dans le manuel MySQL

12.5.1 String Comparison Functions and Operators

Table 12.8 String Comparison Functions and Operators

Name Description
LIKE Simple pattern matching
NOT LIKE Negation of simple pattern matching
STRCMP() Compare two strings

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 LIKE pat [ESCAPE 'escape_char']

    Pattern matching using an SQL pattern. 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:

    1. mysql> SELECT 'ä' LIKE 'ae' COLLATE latin1_german2_ci;
    2. +-----------------------------------------+
    3. | 'ä' LIKE 'ae' COLLATE latin1_german2_ci |
    4. +-----------------------------------------+
    5. |                                       0 |
    6. +-----------------------------------------+
    7. mysql> SELECT 'ä' = 'ae' COLLATE latin1_german2_ci;
    8. +--------------------------------------+
    9. | 'ä' = 'ae' COLLATE latin1_german2_ci |
    10. +--------------------------------------+
    11. |                                    1 |
    12. +--------------------------------------+

    In particular, trailing spaces are significant, which is not true for CHAR or VARCHAR comparisons performed with the = operator:

    1. mysql> SELECT 'a' = 'a ', 'a' LIKE 'a ';
    2. +------------+---------------+
    3. | 'a' = 'a ' | 'a' LIKE 'a ' |
    4. +------------+---------------+
    5. |          1 |             0 |
    6. +------------+---------------+
    7. 1 row in set (0.00 sec)

    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.

    1. mysql> SELECT 'David!' LIKE 'David_';
    2.         -> 1
    3. mysql> SELECT 'David!' LIKE '%D%v%';
    4.         -> 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.

    • \% matches one % character.

    • \_ matches one _ character.

    1. mysql> SELECT 'David!' LIKE 'David\_';
    2.         -> 0
    3. mysql> SELECT 'David_' LIKE 'David\_';
    4.         -> 1

    To specify a different escape character, use the ESCAPE clause:

    1. mysql> SELECT 'David_' LIKE 'David|_' ESCAPE '|';
    2.         -> 1

    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. mysql> SELECT 'abc' LIKE 'ABC';
    2.         -> 1
    3. mysql> SELECT 'abc' LIKE _utf8mb4 'ABC' COLLATE utf8mb4_0900_as_cs;
    4.         -> 0
    5. mysql> SELECT 'abc' LIKE _utf8mb4 'ABC' COLLATE utf8mb4_bin;
    6.         -> 0
    7. mysql> SELECT 'abc' LIKE BINARY 'ABC';
    8.         -> 0

    As an extension to standard SQL, MySQL permits LIKE on numeric expressions.

    1. mysql> SELECT 10 LIKE '1%';
    2.         -> 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.

    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:

    1. mysql> SELECT filename FROM t1;
    2. +--------------+
    3. | filename     |
    4. +--------------+
    5. | C:           |
    6. | C:\          |
    7. | C:\Programs  |
    8. | C:\Programs\ |
    9. +--------------+

    To test for values that end with backslash, you can match the values using either of the following patterns:

    1. mysql> SELECT filename, filename LIKE '%\\' FROM t1;
    2. +--------------+---------------------+
    3. | filename     | filename LIKE '%\\' |
    4. +--------------+---------------------+
    5. | C:           |                   0 |
    6. | C:\          |                   1 |
    7. | C:\Programs  |                   0 |
    8. | C:\Programs\ |                   1 |
    9. +--------------+---------------------+
    10.  
    11. mysql> SELECT filename, filename LIKE '%\\\\' FROM t1;
    12. +--------------+-----------------------+
    13. | filename     | filename LIKE '%\\\\' |
    14. +--------------+-----------------------+
    15. | C:           |                     0 |
    16. | C:\          |                     1 |
    17. | C:\Programs  |                     0 |
    18. | C:\Programs\ |                     1 |
    19. +--------------+-----------------------+
  • expr NOT LIKE pat [ESCAPE 'escape_char']

    This is the same as NOT (expr LIKE pat [ESCAPE 'escape_char']).

    Note

    Aggregate queries involving NOT LIKE comparisons with columns containing NULL may yield unexpected results. For example, consider the following table and data:

    1. CREATE TABLE foo (bar VARCHAR(10));
    2.  

    The query SELECT COUNT(*) FROM foo WHERE bar LIKE '%baz%'; returns 0. You might assume that SELECT COUNT(*) FROM foo WHERE bar NOT LIKE '%baz%'; would return 2. However, this is not the case: The second query returns 0. This is because NULL NOT LIKE expr always returns NULL, regardless of the value of expr. The same is true for aggregate queries involving NULL and comparisons using NOT RLIKE or NOT REGEXP. In such cases, you must test explicitly for NOT NULL using OR (and not AND), as shown here:

    1. SELECT COUNT(*) FROM foo WHERE bar NOT LIKE '%baz%' OR bar IS NULL;
  • STRCMP(expr1,expr2)

    STRCMP() returns 0 if the strings are the same, -1 if the first argument is smaller than the second according to the current sort order, and 1 otherwise.

    1. mysql> SELECT STRCMP('text', 'text2');
    2.         -> -1
    3. mysql> SELECT STRCMP('text2', 'text');
    4.         -> 1
    5. mysql> SELECT STRCMP('text', 'text');
    6.         -> 0

    STRCMP() performs the comparison using the collation of the arguments.

    1. mysql> SET @s1 = _utf8mb4 'x' COLLATE utf8mb4_0900_ai_ci;
    2. mysql> SET @s2 = _utf8mb4 'X' COLLATE utf8mb4_0900_ai_ci;
    3. mysql> SET @s3 = _utf8mb4 'x' COLLATE utf8mb4_0900_as_cs;
    4. mysql> SET @s4 = _utf8mb4 'X' COLLATE utf8mb4_0900_as_cs;
    5. mysql> SELECT STRCMP(@s1, @s2), STRCMP(@s3, @s4);
    6. +------------------+------------------+
    7. | STRCMP(@s1, @s2) | STRCMP(@s3, @s4) |
    8. +------------------+------------------+
    9. |                0 |               -1 |
    10. +------------------+------------------+

    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”.

    1. mysql> SET @s1 = _utf8mb4 'x' COLLATE utf8mb4_0900_ai_ci;
    2. mysql> SET @s2 = _utf8mb4 'X' COLLATE utf8mb4_0900_ai_ci;
    3. mysql> SET @s3 = _utf8mb4 'x' COLLATE utf8mb4_0900_as_cs;
    4. mysql> SET @s4 = _utf8mb4 'X' COLLATE utf8mb4_0900_as_cs;
    5. -->
    6. mysql> SELECT STRCMP(@s1, @s3);
    7. ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT)
    8. and (utf8mb4_0900_as_cs,IMPLICIT) for operation 'strcmp'
    9. mysql> SELECT STRCMP(@s1, @s3 COLLATE utf8mb4_0900_ai_ci);
    10. +---------------------------------------------+
    11. | STRCMP(@s1, @s3 COLLATE utf8mb4_0900_ai_ci) |
    12. +---------------------------------------------+
    13. |                                           0 |
    14. +---------------------------------------------+

Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-string-comparison-functions.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut