- 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
How to obtain multiple rows in a single MySQL query?
To obtain multiple rows in a single MySQL query, use LIKE operator. Let us first create a table −
mysql> create table DemoTable1385 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.90 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1385(Name) values('Chris Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1385(Name) values('Adam Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1385(Name) values('Carol Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1385(Name) values('John Doe'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable1385(Name) values('John Smith'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1385;
This will produce the following output −
+----+--------------+ | Id | Name | +----+--------------+ | 1 | Chris Brown | | 2 | Adam Smith | | 3 | Carol Taylor | | 4 | John Doe | | 5 | John Smith | +----+--------------+ 5 rows in set (0.00 sec)
Here is the query to obtain multiple rows in a single MySQL query −
mysql> select * from DemoTable1385 where Name LIKE '%Smith';
This will produce the following output −
+----+------------+ | Id | Name | +----+------------+ | 2 | Adam Smith | | 5 | John Smith | +----+------------+ 2 rows in set (0.00 sec)
Advertisements