- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Getting count of two different sets of rows in a table and then dividing them in MySQL
For this, use count(*) and the divide the count of two different sets of rows. Let us first create a table −
mysql> create table DemoTable(isMarried tinyint(1)); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.22 sec) 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.07 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | isMarried | +-----------+ | 1 | | 0 | | 1 | | 1 | | 1 | +-----------+ 5 rows in set (0.00 sec)
Following is the query to get the count of two different sets of rows in a table and then dividing them −
mysql> select ( select count(*) from DemoTable where isMarried=1) / ( select count(*) from DemoTable where isMarried=0 ) AS divide from dual;
This will produce the following output −
+--------+ | divide | +--------+ | 4.0000 | +--------+ 1 row in set (0.00 sec)
- Related Articles
- Count number of rows in each table in MySQL?
- Fastest way to count number of rows in MySQL table?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- How to get number of rows in a table without using count(*) MySQL query?
- Getting number of rows in table in an itab in SAP
- Count rows having three or more rows with a certain value in a MySQL table
- How can we use MySQL SELECT statement to count number of rows in a table?
- Get the count of two table fields in a single MySQL query?
- Count two different columns in a single query in MySQL?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Copy all rows of a table to another table in MySQL?
- How to count number of rows in a table with jQuery?
- MySQL - How to count all rows per table in one query?
- Exact count of all rows in MySQL database?
- Getting the last 30 rows in MySQL

Advertisements