MySQL query to select column where value = one or value = two, value = three, etc?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   UserId int,
   UserName varchar(10),
   UserAge int
);
Query OK, 0 rows affected (0.73 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(101,'Chris',23);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(102,'Robert',33);
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values(103,'David',25);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(104,'Carol',35);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(105,'Bob',29);
Query OK, 1 row affected (0.17 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+--------+----------+---------+
| UserId | UserName | UserAge |
+--------+----------+---------+
| 101    | Chris    | 23      |
| 102    | Robert   | 33      |
| 103    | David    | 25      |
| 104    | Carol    | 35      |
| 105    | Bob      | 29      |
+--------+----------+---------+
5 rows in set (0.00 sec)

Following is the query to select the column where value = one or value = two, etc i.e. UserAge in our table with different ages −

mysql> select *from DemoTable where UserAge=25 or UserAge=35 or UserAge=33 or UserAge=29;

This will produce the following output −

+--------+----------+---------+
| UserId | UserName | UserAge |
+--------+----------+---------+
| 102    | Robert   | 33      | 
| 103    | David    | 25      |
| 104    | Carol    | 35      |
| 105    | Bob      | 29      | 
+--------+----------+---------+
4 rows in set (0.00 sec)
raja
Published on 30-Apr-2019 10:17:52
Advertisements