
- 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 rows with a specific column?
Let us first create a table −
mysql> create table DemoTable841( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value varchar(100) ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable841(Value) values('X'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable841(Value) values('Y'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable841(Value) values('Y'); Query OK, 1 row affected (1.62 sec) mysql> insert into DemoTable841(Value) values('Z'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable841(Value) values('X'); Query OK, 1 row affected (0.81 sec) mysql> insert into DemoTable841(Value) values('Z'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable841;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | X | | 2 | Y | | 3 | Y | | 4 | Z | | 5 | X | | 6 | Z | +----+-------+ 6 rows in set (0.00 sec)
Following is the query to count rows with a specific column in MySQL −
mysql> select Value,Concat(COUNT(Value),' Times') from DemoTable841 group by Value;
This will produce the following output −
+-------+-------------------------------+ | Value | Concat(COUNT(Value),' Times') | +-------+-------------------------------+ | X | 2 Times | | Y | 2 Times | | Z | 2 Times | +-------+-------------------------------+ 3 rows in set (0.04 sec)
- Related Articles
- Count and sort rows with a single MySQL query
- Get multiple count in a single MySQL query for specific column values
- MySQL query to get a specific row from rows
- MySQL query to count rows with mutual relation using JOIN?
- MySQL query to count records that begin with specific letters
- How to count the number of occurrences of a specific value in a column with a single MySQL query?
- MySQL query to split a column after specific characters?
- MySQL query to count rows in multiple tables
- Find rows where column value ends with a specific substring in MySQL?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- MySQL random rows sorted by a specific column name?
- How to get the count of a specific value in a column with MySQL?
- MySQL query to count the number of times a specific integer appears in a column for its corresponding value in another column
- How to find repeated rows and display there count in a separate column with MySQL?
- How to count rows from two tables in a single MySQL query?

Advertisements