
- 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
Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
For this, you can use SUM() along with CASE statement. Let us first create a −
mysql> create table DemoTable1430 -> ( -> EmployeeId int, -> isMarried ENUM('YES','NO') -> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1430 values(1001,'Yes'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1430 values(1001,'No'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1430 values(1001,'Yes'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1430 values(1001,'Yes'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select −
mysql> select * from DemoTable1430;
This will produce the following output −
+------------+-----------+ | EmployeeId | isMarried | +------------+-----------+ | 1001 | YES | | 1001 | NO | | 1001 | YES | | 1001 | YES | +------------+-----------+ 4 rows in set (0.00 sec)
Here is the query to select count of value (Yes, No) −
mysql> select EmployeeId,sum(isMarried='Yes') as NumberOfMarried, -> sum(isMarried='No') as NumberOfUnMarried -> from DemoTable1430 -> group by EmployeeId;
This will produce the following output −
+------------+-----------------+-------------------+ | EmployeeId | NumberOfMarried | NumberOfUnMarried | +------------+-----------------+-------------------+ | 1001 | 3 | 1 | +------------+-----------------+-------------------+ 1 row in set (0.00 sec)
- Related Articles
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Set different IDs for records with conditions using a single MySQL query
- How to select different values from same column and display them in different columns with MySQL?
- MySQL query to return the count of only NO values from corresponding column value
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- Add records from corresponding duplicate values in another column with MySQL
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- Get all the records with two different values in another column with MySQL
- How to select sum or 0 if no records exist in MySQL?
- Count only null values in two different columns and display in one MySQL select statement?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Display highest amount from corresponding duplicate ids in MySQL
- How to display the count from distinct records in the same row with MySQL?
- SUM corresponding duplicate records in MySQL

Advertisements