Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

MySQL Select Multiple VALUES?



To select multiple values, you can use where clause with OR and IN operator.

The syntax is as follows −

Case 1 − Using OR

select *from yourTablename where yourColumnName = value1 or yourColumnName = value2 or yourColumnName = value3,.........N;

Case 2 − Using IN

select *from yourTableName where yourColumnName IN(value1,value2,....N);

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

mysql> create table selectMultipleValues
−> (
−> BookId int,
−> BookName varchar(200)
−> );
Query OK, 0 rows affected (1.68 sec)

Now you can insert some records in the table with the help of insert command. The query to insert records is as follows −

mysql> insert into selectMultipleValues values(100,'Introduction to C');
Query OK, 1 row affected (0.18 sec)

mysql> insert into selectMultipleValues values(101,'Introduction to C++');
Query OK, 1 row affected (0.19 sec)

mysql> insert into selectMultipleValues values(103,'Introduction to java');
Query OK, 1 row affected (0.14 sec)

mysql> insert into selectMultipleValues values(104,'Introduction to Python');
Query OK, 1 row affected (0.14 sec)

mysql> insert into selectMultipleValues values(105,'Introduction to C#');
Query OK, 1 row affected (0.13 sec)

mysql> insert into selectMultipleValues values(106,'C in Depth');
Query OK, 1 row affected (0.15 sec)

Display all records from the table with the help of select statement. The query is as follows −

mysql> select *from selectMultipleValues;

The following is the output −

+--------+------------------------+
| BookId | BookName               |
+--------+------------------------+
| 100    | Introduction to C      |
| 101    | Introduction to C++    |
| 103    | Introduction to java   |
| 104    | Introduction to Python |
| 105    | Introduction to C#     |  
| 106    | C in Depth             |
+--------+------------------------+
6 rows in set (0.00 sec)

The following is the query to select multiple values with the help of OR operator.

Case 1 − Using OR operator.

mysql> select *from selectMultipleValues where BookId = 104 or BookId = 106;

The following is the output −

+--------+------------------------+
| BookId | BookName |
+--------+------------------------+
| 104 | Introduction to Python |
| 106 | C in Depth |
+--------+------------------------+
2 rows in set (0.00 sec)

Case 2 − Using In operator.

The following is the query to select multiple values with the help of IN operator.

mysql> select *from selectMultipleValues where BookId in(104,106);

The following is the output −

+--------+------------------------+
| BookId | BookName               |
+--------+------------------------+
| 104    | Introduction to Python |
| 106    | C in Depth             |  
+--------+------------------------+
2 rows in set (0.00 sec)
Updated on: 2020-06-29T09:07:55+05:30

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements