Perform NAND/NOR operations in MySQL


Let us first see how we can perform NAND/NOR operations in MySQL. The concept is as follows −

NAND= NOT( yourColumnName1 AND yourColumnName2)
NOR=NOT( yourColumnName1 OR yourColumnName2)

Let us first create a table −

mysql> create table DemoTable
(
   Value1 boolean ,
   Value2 boolean
);
Query OK, 0 rows affected (0.72 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(true,true);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(false,false);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(false,true);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(true,false);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+--------+
| Value1 | Value2 |
+--------+--------+
|      1 |      1 |
|      0 |      0 |
|      0 |      1 |
|      1 |      0 |
+--------+--------+
4 rows in set (0.00 sec)

Following is the query to perform NAND/NOR operations −

mysql> select Value1,Value2, NOT(Value1 AND Value2) AS NAND_Result,NOT(Value1 OR Value2) AS NOR_Result from DemoTable;

This will produce the following output −

+--------+--------+-------------+------------+
| Value1 | Value2 | NAND_Result | NOR_Result |
+--------+--------+-------------+------------+
|      1 |      1 |           0 |          0 |
|      0 |      0 |           1 |          1 |
|      0 |      1 |           1 |          0 |
|      1 |      0 |           1 |          0 |
+--------+--------+-------------+------------+
4 rows in set (0.00 sec)

Updated on: 09-Oct-2019

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements