
- 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
Count rows having three or more rows with a certain value in a MySQL table
Let us first create a table −
mysql> create table DemoTable -> ( -> UserId int -> ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----------+ | UserId | +----------+ | 10 | | 20 | | 30 | | 10 | | 10 | | 20 | | 30 | | 10 | | 20 | | 20 | | 40 | +----------+ 11 rows in set (0.00 sec)
Following is the query to count the rows having 3 or more rows with a certain value in a MySQL table −
mysql> select count(*) -> from (select UserId, count(*) as total -> from DemoTable group by UserId -> )tbl -> where total >=3;
This will produce the following output i.e. the values 10 and 20 appears three or more times. Therefore, count is for 2 values −
Output
+----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.01 sec)
- Related Articles
- Can I insert two or more rows in a MySQL table at once?
- How to find rows with exact value in one or more columns with MySQL?
- Select rows having more than 2 decimal places in MySQL?
- Delete only specific rows in a table with MySQL
- Fetch random rows from a table with MySQL
- Count number of rows in each table in MySQL?
- Delete more than one rows from a table using id in MySQL?
- How to select the last three rows of a table in ascending order with MySQL?
- MySQL query to count rows with a specific column?
- Count and sort rows with a single MySQL query
- How to count number of rows in a table with jQuery?
- How to replace rows in a MySQL table with conditions?
- How to Count the Number of Rows in a MySQL Table in Python?
- Fastest way to count number of rows in MySQL table?
- Get the number of rows in a particular table with MySQL

Advertisements