
- 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 count the number of 0s and 1s from a table column and display them in two columns?
For this, you can use the aggregate function SUM(). Let us first create a table −
mysql> create table DemoTable ( isMarried tinyint(1) ); Query OK, 0 rows affected (0.84 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(0); 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 −
+-----------+ | isMarried | +-----------+ | 0 | | 1 | | 1 | | 0 | | 1 | | 1 | | 0 | +-----------+ 7 rows in set (0.00 sec)
Following is the query to count the number of 0s and 1s from one column and display them in two columns −
mysql> select sum(tbl.isMarried=1) as all_one_count, sum(tbl.isMarried=0) as all_zero_count, sum(tbl.isMarried in(0,1)) as all_zero_count_and_one_count from DemoTable tbl;
This will produce the following output −
+---------------+----------------+------------------------------+ | all_one_count | all_zero_count | all_zero_count_and_one_count | +---------------+----------------+------------------------------+ | 4 | 3 | 7 | +---------------+----------------+------------------------------+ 1 row in set (0.04 sec)
- Related Articles
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Count Substrings with equal number of 0s, 1s and 2s in C++
- MySQL query to count number of duplicate values in a table column
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Select multiple sums with MySQL query and display them in separate columns?
- How to select different values from same column and display them in different columns with MySQL?
- MySQL query to group by names and display the count in a new column
- Divide numbers from two columns and display result in a new column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Select distinct names from two columns in MySQL and display the result in a single column
- MySQL query to find the number of occurrences from two columns?
- MySQL query to display the count of distinct records from a column with duplicate records
- Multiply values of two columns and display it a new column in MySQL?
- MySQL query to get the length of all columns and display the result in a single new column?

Advertisements