
- 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 get the count of all the elements in the field?
For this, use the COUNT() method. Let us first create a table −
mysql> create table DemoTable -> ( -> ProductName varchar(100) -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Product-1'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Product-4'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-5'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Product-1'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------+ | ProductName | +-------------+ | Product-1 | | Product-2 | | Product-3 | | Product-3 | | Product-2 | | Product-4 | | Product-5 | | Product-1 | +-------------+ 8 rows in set (0.00 sec)
Following is the query to get the count of all the elements in the field −
mysql> select ProductName,count(ProductName) as TotalCount from DemoTable group by ProductName;
Output
This will produce the following output −
+-------------+------------+ | ProductName | TotalCount | +-------------+------------+ | Product-1 | 2 | | Product-2 | 2 | | Product-3 | 2 | | Product-4 | 1 | | Product-5 | 1 | +-------------+------------+ 5 rows in set (0.05 sec)
- Related Articles
- MySQL query to get the count of distinct records in a column
- MySQL query to get the next number in sequence for AUTO_INCREMENT field?
- MongoDB query to create new field and count set the count of another field in it?
- MySQL query to count comma’s from field value?
- MySQL Query to get count of unique values?
- Get the count of two table fields in a single MySQL query?
- MySQL query to get the max value with numeric values in varchar field?
- Should I use COUNT(*) to get all the records in MySQL?
- MySQL query to set current date in the datetime field for all the column values
- A single query to get the sum of count from different tables in MySQL?
- How to count all characters in all rows of a field in MySQL?
- MySQL query to get the character length for all the values in a column?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Which is the fastest method to get the total row count for a MySQL Query?

Advertisements