- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetching multiple MySQL rows based on a specific input within one of the table columns?
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)
Advertisements