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
Selected Reading
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)
Advertisements
