MySQL TINYINT type to return <>1 or IS NULL records

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY ,
   -> EmployeeName varchar(20),
   -> isMarried tinyint
   -> );
Query OK, 0 rows affected (0.83 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(EmployeeName,isMarried) values('Chris',NULL);
Query OK, 1 row affected (0.76 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('David',1);
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Mike',0);
Query OK, 1 row affected (0.69 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Sam',NULL);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Bob',0);
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+--------------+-----------+
| EmployeeId | EmployeeName | isMarried |
+------------+--------------+-----------+
|          1 | Chris        |      NULL |
|          2 | David        |         1 |
|          3 | Mike         |         0 |
|          4 | Sam          |      NULL |
|          5 | Bob          |         0 |
+------------+--------------+-----------+
5 rows in set (0.00 sec)

Here is the query to display records <> 1 or IS NULL −

mysql> select *from DemoTable where isMarried <> 1 or isMarried IS NULL;

This will produce the following output −

+------------+--------------+-----------+
| EmployeeId | EmployeeName | isMarried |
+------------+--------------+-----------+
|          1 | Chris        |      NULL |
|          3 | Mike         |         0 |
|          4 | Sam          |      NULL |
|          5 | Bob          |         0 |
+------------+--------------+-----------+
4 rows in set (0.00 sec)
raja
Published on 13-Dec-2019 07:00:49
Advertisements