Select rows from a MySQL table and display using IN()


Let us first create a table −

mysql> create table DemoTable778 (
   ClientId varchar(100),
   ClientName varchar(100)
);
Query OK, 0 rows affected (1.05 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable778 values('J-101','John');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable778 values('A-102','Adam');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable778 values('C-103','Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable778 values('D-104','David');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable778 values('R-105','Robert');
Query OK, 1 row affected (0.41 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable778;

This will produce the following output -

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| J-101    | John       |
| A-102    | Adam       |
| C-103    | Chris      |
| D-104    | David      |
| R-105    | Robert     |
+----------+------------+
5 rows in set (0.00 sec)

Select rows from a MySQL table and display using IN() −

mysql> select *from DemoTable778 where ClientId IN('J-101','A-102','C-103','D-104','R-105');

This will produce the following output -

+----------+------------+
| ClientId | ClientName |
+----------+------------+
| J-101    | John       |
| A-102    | Adam       |
| C-103    | Chris      |
| D-104    | David      |
| R-105    | Robert     |
+----------+------------+
5 rows in set (0.00 sec)
raja
Published on 09-Sep-2019 11:34:43
Advertisements