Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to find rows with exact value in one or more columns with MySQL?
For this, you can use GROUP BY HAVING with subquery. Let us first create a table −
mysql> create table DemoTable1861 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Marks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1861(Name,Marks) values('John',45);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1861(Name,Marks) values('Chris',74);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1861(Name,Marks) values('David',89);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1861(Name,Marks) values('Chris',74);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1861(Name,Marks) values('John',49);
Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1861;
This will produce the following output −
+----+-------+-------+ | Id | Name | Marks | +----+-------+-------+ | 1 | John | 45 | | 2 | Chris | 74 | | 3 | David | 89 | | 4 | Chris | 74 | | 5 | John | 49 | +----+-------+-------+ 5 rows in set (0.00 sec)
Here is the query to find rows with exact value in one or more columns:
mysql> select Id,Name,Marks from DemoTable1861 where (Name,Marks) IN ( select Name,Marks from DemoTable1861 group by Name,Marks having count(*) > 1);
This will produce the following output −
+----+-------+-------+ | Id | Name | Marks | +----+-------+-------+ | 2 | Chris | 74 | | 4 | Chris | 74 | +----+-------+-------+ 2 rows in set (0.00 sec)
Advertisements
