Skip to content

XOR

Operator Description

The XOR logical operator is used for the *logical XOR operation. If any operand is NULL, the result is NULL; for non-NULLoperands, if there are odd operands that are non-zero, the result istrue, otherwise the result isfalse`.

a XOR b is equal to (a AND (NOT b)) OR ((NOT a) and b) in mathematical operations.

Grammar Structure

> SELECT column_1 XOR column_2 FROM table_name;

Example

mysql> select 1 xor 1;
+-----------+
| 1 xor 1 |
+-----------+
| false |
+-----------+
1 row in set (0.01 sec)

mysql> select 1 xor 0;
+-----------+
| 1 xor 0 |
+-----------+
| true |
+-----------+
1 row in set (0.00 sec)

mysql> select 1 xor null;
+----------------+
| 1 xor null |
+----------------+
| NULL |
+----------------+
1 row in set (0.01 sec)

mysql> select 1 xor 1 xor 1;
+--------------------+
| 1 xor 1 xor 1 |
+--------------------+
| true |
+--------------------+
1 row in set (0.00 sec)
create table t1 (a boolean,b bool);
insert into t1 values ​​(0,1),(true,false),(true,1),(0,false),(NULL,NULL);

mysql> select a xor b from t1;
+-----------+
| a xor b |
+-----------+
| true |
| true |
| false |
| false |
| NULL |
+-----------+
5 rows in set (0.00 sec)