MySQL where column = 'x, y, z'?

MySQLMySQLi Database

You can use IN operator for this.

The syntax is as follows −

SELECT *FROM yourTableName WHERE yourColumnName
IN(‘yourValue1’,‘yourValue2’,‘yourValue3’,...........N);

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table InOperatorDemo
   -> (
   -> ClientId int
   -> );
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into InOperatorDemo values(101);
Query OK, 1 row affected (0.19 sec)
mysql> insert into InOperatorDemo values(110);
Query OK, 1 row affected (0.11 sec)
mysql> insert into InOperatorDemo values(120);
Query OK, 1 row affected (0.17 sec)
mysql> insert into InOperatorDemo values(230);
Query OK, 1 row affected (0.11 sec)
mysql> insert into InOperatorDemo values(270);
Query OK, 1 row affected (0.15 sec)
mysql> insert into InOperatorDemo values(300);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from InOperatorDemo;

The following is The output −

+----------+
| ClientId |
+----------+
| 101      |
| 110      |
| 120      |
| 230      |
| 270      |
| 300      |
+----------+
6 rows in set (0.00 sec)

Here is the query to get value from column with IN operator −

mysql> select *from InOperatorDemo where ClientId IN('120','270','300');

The following is The output −

+----------+
| ClientId |
+----------+
| 120      |
| 270      |
| 300      |
+----------+
3 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 06:42:32
Advertisements