
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Searching 2 fields at the same time to fetch a specific First Name and Last Name from a table in MySQL
For this, you can use LIKE operator along with AND. Let us first create a table −
mysql> create table DemoTable ( EmployeeFirstName varchar(50), EmployeeLastName varchar(50) ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------------+------------------+ | EmployeeFirstName | EmployeeLastName | +-------------------+------------------+ | John | Smith | | David | Miller | | John | Doe | | Adam | Smith | | Carol | Taylor | +-------------------+------------------+ 5 rows in set (0.00 sec)
Following is the query to search 2 fields at the same time to fetch records with FirstName beginning with “Joh” and LastName beginning with “Sm” −
mysql> select *from DemoTable where EmployeeFirstName LIKE 'Joh%' and EmployeeLastName LIKE 'Sm%';
This will produce the following output −
+-------------------+------------------+ | EmployeeFirstName | EmployeeLastName | +-------------------+------------------+ | John | Smith | +-------------------+------------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Fetch a specific column value (name) in MySQL
- Constructing full name from first name, last name and optional middle name in JavaScript
- Searching a specific domain name from URL records in MongoDB?
- MySQL query to display columns name first name, last name as full name in a single column?
- Split First name and Last name using JavaScript?
- Display records where first and last name begins with the same letter in MySQL
- How to filter some fields in objects and fetch a specific subject name value in MongoDB?
- Validate the first name and last name with Java Regular Expressions
- How can create a table having the name like a^b along with same column name? name?
- Fetch domain name by passing name in MySQL?
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- MySQL query to conduct a basic search for a specific last name in a column
- MySQL query to get the last created table name (most recent)?
- Need to update MySQL and SAP database at the same time from a Form
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
Advertisements