Implement MySQL IN for 2 columns to display only selected records


Let us first create a table −

mysql> create table DemoTable810(
   First int,
   Second int
);
Query OK, 0 rows affected (0.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable810 values(20,40);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable810 values(70,90);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable810 values(120,150);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable810 values(78,128);
Query OK, 1 row affected (0.32 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable810 ;

This will produce the following output −

+-------+--------+
| First | Second |
+-------+--------+
|    20 |     40 |
|    70 |     90 |
|   120 |    150 |
|    78 |    128 |
+-------+--------+
4 rows in set (0.00 sec)

Following is the query to implement MySQL IN for 2 columns −

mysql> select *from DemoTable810 where (First,Second) IN((120,150),(20,40));

This will produce the following output −

+-------+--------+
| First | Second |
+-------+--------+
|    20 |     40 |
|   120 |    150 |
+-------+--------+
2 rows in set (0.00 sec)

Updated on: 03-Sep-2019

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements