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)

9. mysql> select 1 xor 0;
10. +---------+
11. | 1 xor 0 |
12. +---------+
13. | true    |
14. +---------+
15. 1 row in set (0.00 sec)

17. mysql> select 1 xor null;
18. +------------+
19. | 1 xor null |
20. +------------+
21. | NULL       |
22. +------------+
23. 1 row in set (0.01 sec)

25. mysql> select 1 xor 1 xor 1;
26. +---------------+
27. | 1 xor 1 xor 1 |
28. +---------------+
29. | true          |
30. +---------------+
31. 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);

4. mysql> select a xor b from t1;
5. +---------+
6. | a xor b |
7. +---------+
8. | true    |
9. | true    |
10. | false   |
11. | false   |
12. | NULL    |
13. +---------+
14. 5 rows in set (0.00 sec)