Passing Multiple ids to single parameter in MySQL?

MySQLMySQLi Database

To pass multiple ids to single parameter, use FIND_IN_SET(). Let us first create a table −

mysql> create table  DemoTable1817
     (
     EmployeeName varchar(20),
     CountryName varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1817 values('Chris','AUS');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1817 values('David','UK');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1817 values('Bob','US');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1817;

This will produce the following output −

+--------------+-------------+
| EmployeeName | CountryName |
+--------------+-------------+
| Chris        |          AUS|
| David        |          UK |
| Bob          |          US |
+--------------+-------------+
3 rows in set (0.00 sec)

Here is the query to pass multiple ids to single parameter in MySQL.

mysql> select * from DemoTable1817
     where find_in_set(CountryName,'US,UK');

This will produce the following output −

+--------------+-------------+
| EmployeeName | CountryName |
+--------------+-------------+
| David        |          UK |
| Bob          |          US |
+--------------+-------------+
2 rows in set (0.00 sec)
raja
Published on 24-Dec-2019 10:53:54
Advertisements