Fetching multiple MySQL rows based on a specific input within one of the table columns?

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Chris','MongoDB');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Bob','MySQL');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('David','Java');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Carol','C');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Sam','Java');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Mike','Java');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1528(StudentName,StudentSubject) values('Adam','MySQL');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1528;

This will produce the following output −

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSubject |
+-----------+-------------+----------------+
|         1 | Chris       | MongoDB        |
|         2 | Bob         | MySQL          |
|         3 | David       | Java           |
|         4 | Carol       | C              |
|         5 | Sam         | Java           |
|         6 | Mike        | Java           |
|         7 | Adam        | MySQL          |
+-----------+-------------+----------------+
7 rows in set (0.00 sec)

Following is the query to fetch multiple MySQL rows based on a specific input within one of the table columns. Here, our specific input is “Java” −

mysql> select StudentId,StudentName,StudentSubject from DemoTable1528 where StudentSubject='Java';

This will produce the following output −

+-----------+-------------+----------------+
| StudentId | StudentName | StudentSubject |
+-----------+-------------+----------------+
|         3 | David       | Java           |
|         5 | Sam         | Java           |
|         6 | Mike        | Java           |
+-----------+-------------+----------------+
3 rows in set (0.00 sec)
raja
Updated on 12-Dec-2019 05:21:22

Advertisements