
- 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
Can I get the count of repeated values in a column with MySQL?
Yes, you can use ORDER BY DESC with GROUP BY. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> PostMessage varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(PostMessage) values('Hi'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(PostMessage) values('Hello'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(PostMessage) values('Hi'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(PostMessage) values('Awesome'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(PostMessage) values('Hello'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(PostMessage) values('Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(PostMessage) values('Awesome'); Query OK, 1 row affected (0.33 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----+-------------+ | Id | PostMessage | +----+-------------+ | 1 | Hi | | 2 | Hello | | 3 | Hi | | 4 | Awesome | | 5 | Hello | | 6 | Hi | | 7 | Awesome | +----+-------------+ 7 rows in set (0.00 sec)
Following is the query to get the highest amount of a value in a MySQL database −
mysql> select PostMessage,count(Id) from DemoTable group by PostMessage -> order by count(Id) DESC;
Output
This will produce the following output −
+-------------+-----------+ | PostMessage | count(Id) | +-------------+-----------+ | Hi | 3 | | Hello | 2 | | Awesome | 2 | +-------------+-----------+ 3 rows in set (0.00 sec)
If you want only highest, you can use the following query −
mysql> select PostMessage,count(Id) from DemoTable group by PostMessage having count(Id) > 2;
Output
This will produce the following output −
+-------------+-----------+ | PostMessage | count(Id) | +-------------+-----------+ | Hi | 3 | +-------------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- Get the maximum count of distinct values in a separate column with MySQL
- Get the count of duplicate values from a single column in MySQL?
- Get count of values that only appear once in a MySQL column?
- Get multiple count in a single MySQL query for specific column values
- How can I get the number of times a specific word appears in a column with MySQL?
- How to get the count of a specific value in a column with MySQL?
- How to find repeated rows and display there count in a separate column with MySQL?
- How can I update MySQL table after quoting the values of a column with a single quote?
- GROUP BY array of document to get the count of repeated Age values
- How can we count a number of unique values in a column in MySQL table?
- How can I count unique records from a column in MySQL database?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- How can I create a MySQL table with a column with only 3 possible given values?
- Can I get Age using BirthDate column in a MySQL query?
- Get the count of only unique rows in a MySQL column?

Advertisements