MySQL query to fetch only a single field on the basis of boolean value in another field

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeName varchar(40),
   isMarried boolean
);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(EmployeeName,isMarried) values('Chris',true);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Robert',false);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Mike',false);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Bob',true);
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable(EmployeeName,isMarried) values('Tom',true);
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 −

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

Following is the query to fetch only a single field on the basis of boolean value in another field −

mysql> select EmployeeName from DemoTable where isMarried=true;

This will produce the following output −

+--------------+
| EmployeeName |
+--------------+
| Chris        |
| Bob          |
| Tom          |
+--------------+
3 rows in set (0.00 sec)
raja
Published on 03-Oct-2019 07:34:08
Advertisements