How to write a single MySQL query for displaying a value for multiple inputs?



For this, use BETWEEN keyword. Let us first create a table −

mysql> create table DemoTable1537    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.72 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1537(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1537(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1537(StudentName) values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1537(StudentName) values('Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1537(StudentName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1537(StudentName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1537(StudentName) values('Carol'); Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1537;

This will produce the following output −

+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ |         1 | Chris       | |         2 | Bob         | |         3 | Sam         | |         4 | Mike        | |         5 | David       | |         6 | John        | |         7 | Carol       | +-----------+-------------+ 7 rows in set (0.00 sec)

Following is the query for displaying a value for multiple inputs −

mysql> select StudentName from DemoTable1537 where StudentId between 3 and 6;

This will produce the following output −

+-------------+ | StudentName | +-------------+ | Sam         | | Mike        | | David       | | John        | +-------------+ 4 rows in set (0.00 sec)
Updated on: 2019-12-12T05:47:28+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements