MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples.

Introduction of MySQL LIKE() function

The LIKE function is a logical function that verifies whether a string contains a mentioned pattern or not.

Syntax of the MySQL LIKE() function

The syntax of the MySQL LIKE() function is given as follows:

expression LIKE pattern ESCAPE escape_character

If the expression matches the pattern in this syntax, the LIKE function returns 1. Otherwise, it returns 0.

MySQL provides two wildcard characters for constructing patterns: percentage % and underscores _.

  • The percentage ( % ) wildcard matches any string of zero or more characters.
  • The underscore ( _ ) wildcard matches any single character.

For example, s% matches any string that starts with the character s, such as sun and six. The se_ matches any string starts with se and is followed by any character such as see and sea.

When the pattern contains the wildcard character, and you want to treat it as a regular character, you can use the ESCAPE clause.

Typically, you'll use the LIKE function in the WHERE clause of the SELECT, DELETE, and UPDATE statement.

Returns:

It will return whether a string contains a specified pattern or not.

Application used for LIKE() function:

The LIKE() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL LIKE() function:

Now we will look into some MySQL LIKE() function examples and will explore how we can use the LIKE function in MySQL.

We will use the following worker table from the sample database for the demonstration:

Worker Table

Worker
Workernumber
Firstname
Lastname
Email
Extension
Email
reportsTo
Officecode
jobname

A) Using MySQL LIKE function with the percentage (%) wildcard examples

This example uses the LIKE function to find workers whose first names start with the letter s:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
firstName LIKE 'a%';

OUTPUT:

WorkernumberFirstnameLastname
1656SasmitaSahoo
1687Sumansahoo

In this example, MySQL scans the whole worker table to find workers whose first names start with the letter ‘s’ and are followed by any number of characters.

The following query uses the LIKE function to find workers whose last names end with the literal string on, e.g., Patterson, Thompson:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
Lastname LIKE '%on';

Here, we used the percentage (%) wildcard at the beginning and the end of the substring to check if a string contains a substring or not.

For example, the following query uses the LIKE function to find all workers whose last names contain the substring on:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
lastname LIKE '%on%';

B) Using MySQL LIKE function with an underscore( _ ) wildcard examples

To find workers whose first names start with the letter T, end with the letter m, and contain any single character between, e.g., Tom, Tim, you use the underscore (_) wildcard to construct the pattern as follows:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
firstname LIKE 'T_m';

C) Using MySQL NOT LIKE function example

The MySQL allows you to combine the NOT function with the LIKE function to find a string that does not match a specific pattern.

Suppose you want to search for workers whose last names don't start with the letter B; you can use the NOT LIKE function as follows:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
lastName NOT LIKE 'B%';

It should be noted that the pattern is not case-sensitive. Therefore, the b% and B% patterns return the same result.

MySQL LIKE function with the ESCAPE clause

Sometimes the pattern may contain wildcard characters, e.g., 10%, _20, etc.

In this case, you can use the ESCAPE clause to specify the escape character so that the LIKE function interprets the wildcard character as a literal character.

If you don't specify the escape character explicitly, the backslash character (\) is the default escape character.

For example, if you want to find products whose product codes contain the string _20, you can use the pattern %\_20% with the default escape character:

SELECT
productCode,
productName
FROM
products
WHERE
productCode LIKE '%\_20%';

Alternatively, you can specify a different escape character, e.g., $ using the ESCAPE clause:

SELECT
productCode,
productName
FROM
products
WHERE
productCode LIKE '%$_20%' ESCAPE '$';

The pattern %$_20% matches any string that contains the _20 string.

Application of MySQL LIKE() function:

This function is used to test whether a string contains a specified pattern or not.

Summary

In the above context, we have learned how we can use the LIKE() function in MySQL

to Use the LIKE function to test if a value matches a pattern.

The % wildcard matches zero or more characters.

The _ wildcard matches a single character.

Use ESCAPE clause specifies an escape character other than the default escape character (\).

Use the NOT function to negate the LIKE function.