SELECT * WHERE var == [one of many alternatives] in MySQL?


Use IN() for select * where var== [one of many alternatives]. Let us first create a table −

mysql> create table DemoTable1624
    -> (
    -> ClientId int,
    -> ClientName varchar(20)
    -> );
Query OK, 0 rows affected (0.39 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1624 values(101,'Chris Brown');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1624 values(102,'David Miller');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1624 values(103,'John Smith');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1624 values(104,'Carol Taylor');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1624 values(105,'Adam Smith');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1624;

This will produce the following output −

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
|      101 | Chris Brown  |
|      102 | David Miller |
|      103 | John Smith   |
|      104 | Carol Taylor |
|      105 | Adam Smith   |
+----------+--------------+
5 rows in set (0.00 sec)

Here is the query to implement SELECT * WHERE var == [one of many alternatives] −

mysql> select * from DemoTable1624 where ClientId in(101,102,104);

This will produce the following output −

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
|      101 | Chris Brown  |
|      102 | David Miller |
|      104 | Carol Taylor |
+----------+--------------+
3 rows in set (0.00 sec)

Updated on: 08-Nov-2019

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements