- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to select a record with two exact values?
For this, you can use GROUP BY HAVING clause. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(800); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | 600 | | 2 | 600 | | 3 | 800 | | 4 | 800 | +----+-------+ 4 rows in set (0.00 sec)
Here is the query to select a record with two exact values −
mysql> select Value from DemoTable tbl where Id IN(1,2) group by Value having count(distinct Id)=2;
This will produce the following output −
+-------+ | Value | +-------+ | 600 | +-------+ 1 row in set (0.07 sec)
- Related Articles
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL SELECT from two tables with a single query
- How to get a specific column record from SELECT query in MySQL?
- MySQL query to search record with apostrophe?
- MySQL query to select column values ending with certain character/number?
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- MySQL query to delete a record with the lowest ID?
- MySQL select and insert in two tables with a single query
- MySQL query to select a count on two separate conditions?
- MySQL select * and find record with current date
- Insert with a Select query in MySQL
- MySQL query to select records with a particular date?
- MySQL query to search exact word from string?
- Insert record using MySQL SELECT?
- Select query to display duplicate values with max date

Advertisements