| by Arround The Web | No comments

How to Get The Number Of Records or Rows In a Table Using MySQL

MySQL is an open-source powerful RDMS, which contains many scaleable Tables to store the data in an organized structure. While managing a database, there is a need to retrieve the number of records or rows in a table, you can do this by using the COUNT() function, the information schema, or the SHOW TABLE STATUS command. However, the COUNT() function is the easiest method for this purpose.

This article will teach how to get the number of Records or Rows in a Table using the MySQL COUNT() Function in the Select query.

Prerequisite: Connect to Local MySQL Server

Make sure that MySQL is installed in your system, to confirm whether MySQL is installed in your system or not, type this command:

mysql --version

 

Here it is visible in the output above that MySQL is installed in the system.

Connect to the Local MySQL server by using this syntax:

mysql -u <username> -p

 
Provide your username, the username for this post is “md”, type your server password to log in, successfully:


Use this command for available databases on the server:

SHOW DATABASES;

 

To work in a specific database, use this syntax:

use <database-name>;

 

A success message will appear.

Use this command to display all the tables available in the database:

Get The Number Of Records or Rows Using the COUNT Function In a Table

The COUNT() function can be used in combination with other functions and operators in MySQL. If you provide “*” to the COUNT function it will return the number of all Records or Rows in that table, to do so use this syntax:

SELECT COUNT(*) FROM <table_name>;

 

You got the total count of all the records or rows of the table.

Let’s insert a new Record in the table using the INSERT Command and then again use this COUNT Function command, to get the number of records or rows in the table:


If you want to count the number of rows for a specific column, use this syntax:

SELECT COUNT<column_name> FROM <table_name>;

 
Provide the name of the table and column:


To use the COUNT() function with a WHERE clause to get the number of rows that meet specific criteria, use this syntax:

SELECT COUNT(*) FROM <table_name> WHERE <column_name> = value;

 
Provide the parameters and criteria:


You are able to get The Number Of Records or Rows In a Table Using the COUNT() function of MySQL.

Conclusion

Getting the number of Records or Rows in a MySQL Table is an important task for database management sometimes, which can be easily accomplished using the COUNT() function. It helps you to count the number of all the Records in a table or from a specific column. It can also be used in combination with other functions and operators in MySQL to provide the count of records or rows that meet a specific condition.

Share Button

Source: linuxhint.com

Leave a Reply