
- 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 Select where value exists more than once
For this, you can use GROUP BY HAVING along with the COUNT(*) function. Let us first create a table −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 20 | | 10 | | 30 | | 10 | | 30 | | 40 | | 50 | +-------+ 7 rows in set (0.00 sec)
Following is the query to select where value exists more than once −
mysql> select *from DemoTable -> group by Value -> having count(*) > 1;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 30 | +-------+ 2 rows in set (0.38 sec)
- Related Articles
- MySQL query to find a value appearing more than once?
- Select MySQL rows where column contains same data in more than one record?
- How to check if value exists with MySQL SELECT 1?
- Array elements that appear more than once?
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- SELECT where row value contains string in MySQL?
- Select rows having more than 2 decimal places in MySQL?
- Array elements that appear more than once in C?
- Select from table where value does not exist with MySQL?
- MySQL query to select column where value = one or value = two, value = three, etc?
- MySQL query to count where more than three columns values are true?
- MySQL SELECT products WHERE 'average price per product' < value?
- How to select where sum of fields is greater than a value in MongoDB?
- addEventListener() not working more than once with a button in JavaScript?
- Insert more than one element at once in a C# List

Advertisements