Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

12.3.3 Logical Operators

Table 12.4 Logical Operators

Name Description
AND, && Logical AND
NOT, ! Negates value
||, OR Logical OR
XOR Logical XOR

In SQL, all logical operators evaluate to TRUE, FALSE, or NULL (UNKNOWN). In MySQL, these are implemented as 1 (TRUE), 0 (FALSE), and NULL. Most of this is common to different SQL database servers, although some servers may return any nonzero value for TRUE.

MySQL evaluates any nonzero, non-NULL value to TRUE. For example, the following statements all assess to TRUE:

  1. mysql> SELECT 10 IS TRUE;
  2. -> 1
  3. mysql> SELECT -10 IS TRUE;
  4. -> 1
  5. mysql> SELECT 'string' IS NOT NULL;
  6. -> 1
  • NOT, !

    Logical NOT. Evaluates to 1 if the operand is 0, to 0 if the operand is nonzero, and NOT NULL returns NULL.

    1. mysql> SELECT NOT 10;
    2.         -> 0
    3. mysql> SELECT NOT 0;
    4.         -> 1
    5. mysql> SELECT NOT NULL;
    6.         -> NULL
    7. mysql> SELECT ! (1+1);
    8.         -> 0
    9. mysql> SELECT ! 1+1;
    10.         -> 1

    The last example produces 1 because the expression evaluates the same way as (!1)+1.

  • AND, &&

    Logical AND. Evaluates to 1 if all operands are nonzero and not NULL, to 0 if one or more operands are 0, otherwise NULL is returned.

    1. mysql> SELECT 1 AND 1;
    2.         -> 1
    3. mysql> SELECT 1 AND 0;
    4.         -> 0
    5. mysql> SELECT 1 AND NULL;
    6.         -> NULL
    7. mysql> SELECT 0 AND NULL;
    8.         -> 0
    9. mysql> SELECT NULL AND 0;
    10.         -> 0
  • OR, ||

    Logical OR. When both operands are non-NULL, the result is 1 if any operand is nonzero, and 0 otherwise. With a NULL operand, the result is 1 if the other operand is nonzero, and NULL otherwise. If both operands are NULL, the result is NULL.

    1. mysql> SELECT 1 OR 1;
    2.         -> 1
    3. mysql> SELECT 1 OR 0;
    4.         -> 1
    5. mysql> SELECT 0 OR 0;
    6.         -> 0
    7. mysql> SELECT 0 OR NULL;
    8.         -> NULL
    9. mysql> SELECT 1 OR NULL;
    10.         -> 1
  • XOR

    Logical XOR. Returns NULL if either operand is NULL. For non-NULL operands, evaluates to 1 if an odd number of operands is nonzero, otherwise 0 is returned.

    1. mysql> SELECT 1 XOR 1;
    2.         -> 0
    3. mysql> SELECT 1 XOR 0;
    4.         -> 1
    5. mysql> SELECT 1 XOR NULL;
    6.         -> NULL
    7. mysql> SELECT 1 XOR 1 XOR 1;
    8.         -> 1

    a XOR b is mathematically equal to (a AND (NOT b)) OR ((NOT a) and b).


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-logical-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