Rechercher dans le manuel MySQL

12.3.2 Comparison Functions and Operators

Table 12.3 Comparison Operators

Name Description
BETWEEN ... AND ... Check whether a value is within a range of values
COALESCE() Return the first non-NULL argument
= Equal operator
<=> NULL-safe equal to operator
> Greater than operator
>= Greater than or equal operator
GREATEST() Return the largest argument
IN() Check whether a value is within a set of values
INTERVAL() Return the index of the argument that is less than the first argument
IS Test a value against a boolean
IS NOT Test a value against a boolean
IS NOT NULL NOT NULL value test
IS NULL NULL value test
ISNULL() Test whether the argument is NULL
LEAST() Return the smallest argument
< Less than operator
<= Less than or equal operator
LIKE Simple pattern matching
NOT BETWEEN ... AND ... Check whether a value is not within a range of values
!=, <> Not equal operator
NOT IN() Check whether a value is not within a set of values
NOT LIKE Negation of simple pattern matching
STRCMP() Compare two strings

Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary.

The following relational comparison operators can be used to compare not only scalar operands, but row operands:

  1. =  >  <  >=  <=  <>  !=

The descriptions for those operators later in this section detail how they work with row operands. For additional examples of row comparisons in the context of row subqueries, see Section 13.2.11.5, “Row Subqueries”.

Some of the functions in this section return values other than 1 (TRUE), 0 (FALSE), or NULL. LEAST() and GREATEST() are examples of such functions; Section 12.2, “Type Conversion in Expression Evaluation”, describes the rules for comparison operations performed by these and similar functions for determining their return values.

Note

In previous versions of MySQL, when evaluating an expression containing LEAST() or GREATEST(), the server attempted to guess the context in which the function was used, and to coerce the function's arguments to the data type of the expression as a whole. For example, the arguments to LEAST("11", "45", "2") are evaluated and sorted as strings, so that this expression returns "11". In MySQL 8.0.3 and earlier, when evaluating the expression LEAST("11", "45", "2") + 0, the server converted the arguments to integers (anticipating the addition of integer 0 to the result) before sorting them, thus returning 2.

Beginning with MySQL 8.0.4, the server no longer attempts to infer context in this fashion. Instead, the function is executed using the arguments as provided, performing data type conversions to one or more of the arguments if and only if they are not all of the same type. Any type coercion mandated by an expression that makes use of the return value is now performed following function execution. This means that, in MySQl 8.0.4 and later, LEAST("11", "45", "2") + 0 evaluates to "11" + 0 and thus to integer 11. (Bug #83895, Bug #25123839)

To convert a value to a specific type for comparison purposes, you can use the CAST() function. String values can be converted to a different character set using CONVERT(). See Section 12.10, “Cast Functions and Operators”.

By default, string comparisons are not case-sensitive and use the current character set. The default is utf8mb4.

  • =

    Equal:

    1. mysql> SELECT 1 = 0;
    2.         -> 0
    3. mysql> SELECT '0' = 0;
    4.         -> 1
    5. mysql> SELECT '0.0' = 0;
    6.         -> 1
    7. mysql> SELECT '0.01' = 0;
    8.         -> 0
    9. mysql> SELECT '.01' = 0.01;
    10.         -> 1

    For row comparisons, (a, b) = (x, y) is equivalent to:

    1. (a = x) AND (b = y)
  • <=>

    NULL-safe equal. This operator performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is NULL.

    The <=> operator is equivalent to the standard SQL IS NOT DISTINCT FROM operator.

    1. mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
    2.         -> 1, 1, 0
    3. mysql> SELECT 1 = 1, NULL = NULL, 1 = NULL;
    4.         -> 1, NULL, NULL

    For row comparisons, (a, b) <=> (x, y) is equivalent to:

    1. (a <=> x) AND (b <=> y)
  • <>, !=

    Not equal:

    1. mysql> SELECT '.01' <> '0.01';
    2.         -> 1
    3. mysql> SELECT .01 <> '0.01';
    4.         -> 0
    5. mysql> SELECT 'zapp' <> 'zappp';
    6.         -> 1

    For row comparisons, (a, b) <> (x, y) and (a, b) != (x, y) are equivalent to:

    1. (a <> x) OR (b <> y)
  • <=

    Less than or equal:

    1. mysql> SELECT 0.1 <= 2;
    2.         -> 1

    For row comparisons, (a, b) <= (x, y) is equivalent to:

    1. (a < x) OR ((a = x) AND (b <= y))
  • <

    Less than:

    1. mysql> SELECT 2 < 2;
    2.         -> 0

    For row comparisons, (a, b) < (x, y) is equivalent to:

    1. (a < x) OR ((a = x) AND (b < y))
  • >=

    Greater than or equal:

    1. mysql> SELECT 2 >= 2;
    2.         -> 1

    For row comparisons, (a, b) >= (x, y) is equivalent to:

    1. (a > x) OR ((a = x) AND (b >= y))
  • >

    Greater than:

    1. mysql> SELECT 2 > 2;
    2.         -> 0

    For row comparisons, (a, b) > (x, y) is equivalent to:

    1. (a > x) OR ((a = x) AND (b > y))
  • IS boolean_value

    Tests a value against a boolean value, where boolean_value can be TRUE, FALSE, or UNKNOWN.

    1. mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;
    2.         -> 1, 1, 1
  • IS NOT boolean_value

    Tests a value against a boolean value, where boolean_value can be TRUE, FALSE, or UNKNOWN.

    1. mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;
    2.         -> 1, 1, 0
  • IS NULL

    Tests whether a value is NULL.

    1. mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL;
    2.         -> 0, 0, 1

    To work well with ODBC programs, MySQL supports the following extra features when using IS NULL:

  • IS NOT NULL

    Tests whether a value is not NULL.

    1.         -> 1, 1, 0
  • expr BETWEEN min AND max

    If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.

    1. mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;
    2.         -> 1, 0
    3. mysql> SELECT 1 BETWEEN 2 AND 3;
    4.         -> 0
    5. mysql> SELECT 'b' BETWEEN 'a' AND 'c';
    6.         -> 1
    7. mysql> SELECT 2 BETWEEN 2 AND '3';
    8.         -> 1
    9. mysql> SELECT 2 BETWEEN 2 AND 'x-3';
    10.         -> 0

    For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.

  • expr NOT BETWEEN min AND max

    This is the same as NOT (expr BETWEEN min AND max).

  • COALESCE(value,...)

    Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

    The return type of COALESCE() is the aggregated type of the argument types.

    1. mysql> SELECT COALESCE(NULL,1);
    2.         -> 1
    3.         -> NULL
  • GREATEST(value1,value2,...)

    With two or more arguments, returns the largest (maximum-valued) argument. The arguments are compared using the same rules as for LEAST().

    1. mysql> SELECT GREATEST(2,0);
    2.         -> 2
    3. mysql> SELECT GREATEST(34.0,3.0,5.0,767.0);
    4.         -> 767.0
    5. mysql> SELECT GREATEST('B','A','C');
    6.         -> 'C'

    GREATEST() returns NULL if any argument is NULL.

  • expr IN (value,...)

    Returns 1 if expr is equal to any of the values in the IN list, else returns 0. If all values are constants, they are evaluated according to the type of expr and sorted. The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the arguments.

    1. mysql> SELECT 2 IN (0,3,5,7);
    2.         -> 0
    3. mysql> SELECT 'wefwf' IN ('wee','wefwf','weg');
    4.         -> 1

    IN can be used to compare row constructors:

    1. mysql> SELECT (3,4) IN ((1,2), (3,4));
    2.         -> 1
    3. mysql> SELECT (3,4) IN ((1,2), (3,5));
    4.         -> 0

    You should never mix quoted and unquoted values in an IN list because the comparison rules for quoted values (such as strings) and unquoted values (such as numbers) differ. Mixing types may therefore lead to inconsistent results. For example, do not write an IN expression like this:

    1. SELECT val1 FROM tbl1 WHERE val1 IN (1,2,'a');

    Instead, write it like this:

    1. SELECT val1 FROM tbl1 WHERE val1 IN ('1','2','a');

    The number of values in the IN list is only limited by the max_allowed_packet value.

    To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.

    IN() syntax can also be used to write certain types of subqueries. See Section 13.2.11.3, “Subqueries with ANY, IN, or SOME”.

  • expr NOT IN (value,...)

    This is the same as NOT (expr IN (value,...)).

  • ISNULL(expr)

    If expr is NULL, ISNULL() returns 1, otherwise it returns 0.

    1. mysql> SELECT ISNULL(1+1);
    2.         -> 0
    3. mysql> SELECT ISNULL(1/0);
    4.         -> 1

    ISNULL() can be used instead of = to test whether a value is NULL. (Comparing a value to NULL using = always yields NULL.)

    The ISNULL() function shares some special behaviors with the IS NULL comparison operator. See the description of IS NULL.

  • INTERVAL(N,N1,N2,N3,...)

    Returns 0 if N < N1, 1 if N < N2 and so on or -1 if N is NULL. All arguments are treated as integers. It is required that N1 < N2 < N3 < ... < Nn for this function to work correctly. This is because a binary search is used (very fast).

    1. mysql> SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);
    2.         -> 3
    3. mysql> SELECT INTERVAL(10, 1, 10, 100, 1000);
    4.         -> 2
    5. mysql> SELECT INTERVAL(22, 23, 30, 44, 200);
    6.         -> 0
  • LEAST(value1,value2,...)

    With two or more arguments, returns the smallest (minimum-valued) argument. The arguments are compared using the following rules:

    • If any argument is NULL, the result is NULL. No comparison is needed.

    • If all arguments are integer-valued, they are compared as integers.

    • If at least one argument is double precision, they are compared as double-precision values. Otherwise, if at least one argument is a DECIMAL value, they are compared as DECIMAL values.

    • If the arguments comprise a mix of numbers and strings, they are compared as numbers.

    • If any argument is a nonbinary (character) string, the arguments are compared as nonbinary strings.

    • In all other cases, the arguments are compared as binary strings.

    The return type of LEAST() is the aggregated type of the comparison argument types.

    1. mysql> SELECT LEAST(2,0);
    2.         -> 0
    3. mysql> SELECT LEAST(34.0,3.0,5.0,767.0);
    4.         -> 3.0
    5. mysql> SELECT LEAST('B','A','C');
    6.         -> 'A'

Rechercher dans le manuel MySQL

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-comparison-operators.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

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

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.

Table des matières Haut