Like Operator in MySQL
Hello and
welcome everyone. In this article, we will learn about the Like Operator in
MySQL. Let’s go.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
MySQL
LIKE operator is used with WHERE clause of SELECT, DELETE, UPDATE statement to search
for a specified pattern in a column. LIKE operator allows us to perform pattern
matching and were want to do an exact match. Following are the two wildcard
characters provided in MySQL that creating patterns,
Underscore
( _ ) – this wild card matches a single character from the table. For example,
suppose we gave pattern like te_, now any string starts with te from the table
then the character will display like tea or tennis or teen.
Percentage
( % ) – this wild card matches a string of zero or more characters. For example
suppose we given pattern such as T%, now any string starts with T from the
table then the character will display like Table or Tea or Tiger.
Syntax:
SELECT
column_name_1, column_name_2, . . .
FROM
table_name
WHERE
condition
expr
LIKE pattern [ ESCAPE ‘escape_char’ ]
Here is
expr is a column character expression
the pattern is matching character expression.
For example
Here is
the, we will create a table for an employee in the database.
CREATE
TABLE IF NOT EXISTS employee (
e_id
int (10) PRIMARY KEY,
e_firstname
varchar (50) NOT NULL,
e_lastname
varchar (50) NOT NULL,
e_age
int (10),
e_city
varchar (250)
e_sex
char (1)
e_phnumber
int (15) NOT NULL
)
Here we
insert record in the employee table.
INSERT INTO employee (
‘e_id’, ’e_firstname’, ’e_lastname’,
’e_age’, ’e_city’, ’e_sex’,
’e_phnumber’
)
VALUES
(
1, ‘Hone’, ‘Reus’, 26, ‘San Jose’, ‘M’, ‘ +
1 555 555 1111’
),
(
2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’
+1 555 555 1111’
);
Final
Result
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’ ’e_phnumber’
1
Hone Reus 26 San Jose M
+1 555 555 1111
2 Cries Fence 23 Fresno F
+1 555 555 1111
Example 1:
Now I want to display employee first name
starting with “C”:
SELECT
* FROM employee
WHERE
e_firstname LIKE ‘C%’;
Example 2:
Now I want to display employee first name
character ends with “e” or that name not start with “e”:
SELECT
* FROM employee
WHERE
e_firstname LIKE ‘%e’;
Example 3:
Now I want to display employee last name that has
“en” at any position:
SELECT
* FROM employee
WHERE
e_lastname LIKE ‘%en%’;
Example 4:
Now I want to display all employee last names
that have e in the second position:
SELECT
* FROM employee
WHERE
e_lastname LIKE ‘_e’;
In this article, we learned about LIKE operator
with two wildcards like percentage ( % ) and Underscore ( _ ) with example.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
Comments
Post a Comment