
- 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
Limit the count using GROUP BY in MySQL
Let us first create a table −
mysql> create table DemoTable ( UserId int, UserMessage varchar(100) ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1,'Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(2,'Hello'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(2,'Good'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1,'Nice'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(1,'Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1,'Amazing'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1,'Good Morning'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+--------------+ | UserId | UserMessage | +--------+--------------+ | 1 | Hi | | 2 | Hello | | 2 | Good | | 1 | Nice | | 1 | Awesome | | 1 | Amazing | | 1 | Good Morning | +--------+--------------+ 7 rows in set (0.00 sec)
Following is the query to limit the count using GROUP BY −
mysql> select UserId, case when count(*) < 4 then count(*) else 'Greater Than 4' end as 'NumberOfMessage' from DemoTable group by UserId;
This will produce the following output −
+--------+-----------------+ | UserId | NumberOfMessage | +--------+-----------------+ | 1 | Greater Than 4 | | 2 | 2 | +--------+-----------------+ 2 rows in set (0.06 sec)
- Related Articles
- Using group by on two fields and count in MySQL?
- MySQL- GROUP and COUNT by date?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL GROUP BY date when using datetime?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- MySQL query to group by names and display the count in a new column
- Pagination using MySQL LIMIT, OFFSET?
- MongoDB SELECT COUNT GROUP BY?
- Using GROUP BY and MAX on multiple columns in MySQL?
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- MySQL query to group results by date and display the count of duplicate values?
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- SELECT last entry without using LIMIT in MySQL?
- Listing all rows by group with MySQL GROUP BY?
- What is the significance of using multiple columns in MySQL GROUP BY clause?

Advertisements