XOR

Description

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

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

Syntax

  1. > SELECT column_1 XOR column_2 FROM table_name;

Examples

  1. mysql> select 1 xor 1;
  2. +---------+
  3. | 1 xor 1 |
  4. +---------+
  5. | false |
  6. +---------+
  7. 1 row in set (0.01 sec)
  8. mysql> select 1 xor 0;
  9. +---------+
  10. | 1 xor 0 |
  11. +---------+
  12. | true |
  13. +---------+
  14. 1 row in set (0.00 sec)
  15. mysql> select 1 xor null;
  16. +------------+
  17. | 1 xor null |
  18. +------------+
  19. | NULL |
  20. +------------+
  21. 1 row in set (0.01 sec)
  22. mysql> select 1 xor 1 xor 1;
  23. +---------------+
  24. | 1 xor 1 xor 1 |
  25. +---------------+
  26. | true |
  27. +---------------+
  28. 1 row in set (0.00 sec)
  1. create table t1 (a boolean,b bool);
  2. insert into t1 values (0,1),(true,false),(true,1),(0,false),(NULL,NULL);
  3. mysql> select a xor b from t1;
  4. +---------+
  5. | a xor b |
  6. +---------+
  7. | true |
  8. | true |
  9. | false |
  10. | false |
  11. | NULL |
  12. +---------+
  13. 5 rows in set (0.00 sec)