MySQL Select Multiple Values

Maybe one of the most used MySQL commands is SELECT. That is the way to extract the information from the database, but of course, one does not need all the info inside a database. Therefore one should limit the info coming out from the table. There is a WHERE statement comes into play, with it, one can limit the data to only the one that complies with a certain condition.

In this tutorial, you will learn how to use a single MySQL statement to select multiple values.

How to Select Multiple Values

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

Example 1 - Using OR

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

Example 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 into the table with the help of the 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 a 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 the OR operator.

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)
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)


Leave a reply



Submit