
- 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
Get rows that have common value from the same table with different id in MySQL
For this, you can use GROUP BY HAVING clause. Let us first create a table −
mysql> create table DemoTable1467 -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1467 values(100,'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1467 values(110,'David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1467 values(120,'Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1467 values(100,'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1467 values(130,'Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1467 values(100,'Chris'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1467;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 100 | Chris | | 110 | David | | 120 | Mike | | 100 | Chris | | 130 | Carol | | 100 | Chris | +------+-------+ 6 rows in set (0.00 sec)
Here is the query to get rows that have common value from the same table with different id −
mysql> select Id,Name,count(*) as IdCount from DemoTable1467 -> group by Id -> having IdCount > 2;
This will produce the following output −
+------+-------+---------+ | Id | Name | IdCount | +------+-------+---------+ | 100 | Chris | 3 | +------+-------+---------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL - SUM rows with same ID?
- Find rows that have the same value on a column in MySQL?
- Count how many rows have the same value in MySQL?
- Can different HTML elements have the same ID?
- MySQL query to find the average of rows with the same ID
- How to return rows that have the same column values in MySQL?
- Delete more than one rows from a table using id in MySQL?
- MySQL Select Rows where two columns do not have the same value?
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- Get the number of rows in a particular table with MySQL
- How to get username using ID from another table in MySQL database?
- How can we get randomly different set of rows or values each time from MySQL table?
- How to get the data associated with the maximum id in a MySQL table?
- Get MySQL maximum value from 3 different columns?
- Fetch random rows from a table with MySQL

Advertisements