MySQL XOR operator with IN clause?

MySQLMySQLi Database

MySQL XOR returns TRUE if one or the other operand (or expression) but not both is TRUE. The IN clause is used to specify a condition with any other MySQL query.

Let us first create a table

mysql> create table DemoTable
-> (
-> Num1 int,
-> Num2 int
-> );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(5,5);
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values(6,6);
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values(7,7);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------+------+
| Num1 | Num2 |
+------+------+
| 5    | 5    |
| 6    | 6    |
| 7    | 7    |
+------+------+
3 rows in set (0.00 sec)

Following is the query for XOR with IN in MySQL −

mysql> select Num1,Num2 from DemoTable where ( Num1 IN (5,6,7 ) XOR Num2 IN ( 16, 16, 16 ) );

Output

This will produce the following output −

+------+------+
| Num1 | Num2 |
+------+------+
| 5    | 5    |
| 6    | 6    |
| 7    | 7    |
+------+------+
3 rows in set (0.00 sec)
raja
Published on 12-Jul-2019 10:40:51
Advertisements