- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Which is the fastest method to get the total row count for a MySQL Query?
You can use subquery with aggregate COUNT(*) to get the total row count.
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentAge int ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName,StudentAge) values('John',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Larry',21); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Johnny',23); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Chris',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('David',23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('James',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Jace',24); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Mike',21); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Robert',25); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Mike',22); Query OK, 1 row affected (0.23 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+------------+ | StudentId | StudentFirstName | StudentAge | +-----------+------------------+------------+ | 1 | John | 23 | | 2 | Larry | 21 | | 3 | Johnny | 23 | | 4 | Chris | 21 | | 5 | David | 23 | | 6 | James | 21 | | 7 | Jace | 24 | | 8 | Mike | 21 | | 9 | Robert | 25 | | 10 | Mike | 22 | +-----------+------------------+------------+ 10 rows in set (0.00 sec)
Following is the query to get total row count for a MySQL Query and set for all the column values −
mysql> select StudentFirstName,(select count(*) from DemoTable) AS Total_Count from DemoTable limit 10;
This will produce the following output −
+------------------+-------------+ | StudentFirstName | Total_Count | +------------------+-------------+ | John | 10 | | Larry | 10 | | Johnny | 10 | | Chris | 10 | | David | 10 | | James | 10 | | Jace | 10 | | Mike | 10 | | Robert | 10 | | Mike | 10 | +------------------+-------------+ 10 rows in set (0.03 sec)
Advertisements