
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to fetch only a single field on the basis of boolean value in another field
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)
- Related Articles
- Count boolean field values within a single MySQL query?
- MySQL query to update only a single field in place of NULL
- MongoDB query to fetch only the “Name” field based on roles?
- Is there a way in MySQL to reverse a boolean field with a single query?
- How to derive value of a field from another field in MySQL?
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- Concatenate rows on the basis of boolean values in another column with MySQL
- MongoDB query for a single field
- Update MongoDB field using value of another field?
- MySQL query to count comma’s from field value?
- How to run MongoDB query to update only a specific field value?
- Selecting only a single field from MongoDB?
- Unable to implement $addToSet in MongoDB to fetch values of a single field?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Perform SUM and SUBTRACTION on the basis of a condition in a single MySQL query?

Advertisements