The LIKE operator performs pattern matching using an SQL pattern.
If the string matches the pattern given, the result is 1(TRUE), otherwise it’s 0(FALSE).
This is not necessarily to be a literal string. For example, it can be specified as a string expression or table column or numeric expressions.
With LIKE we can use the following two wildcard characters in the pattern:
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
1 (TRUE), If the string matches the pattern given.
0(FALSE), If the string doesn’t matches the pattern given.
NULL, If either expression or patstring is NULL.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]
Below is an example of how to use this operator in a SELECT statement:
mysql> SELECT 'Tutorialmines' LIKE 'Tut%'; +-----------------------------+ | 'Tutorialmines' LIKE 'Tut%' | +-----------------------------+ | 1 | +-----------------------------+ 1 row in set (0.00 sec)
In Above example, the return value is 1. It means that the input string matched the pattern. We specified that the input string should start with Tut and end with anything. The % wildcard character matches any number of characters (including zero characters).
Below will happens if we drop the wildcard %:
mysql> SELECT 'Tutorialmines' LIKE 'Tut'; +----------------------------+ | 'Tutorialmines' LIKE 'Tut' | +----------------------------+ | 0 | +----------------------------+ 1 row in set (0.00 sec)
In Above example, the return value is 0. It means that the input string doesn’t matched the pattern.
We can use this option also i.e. _ wildcard character. It will match only a one character. Here’s an example:
mysql> SELECT 'Tutorialmines' LIKE 'Tut_rialmines'; +--------------------------------------+ | 'Tutorialmines' LIKE 'Tut_rialmines' | +--------------------------------------+ | 1 | +--------------------------------------+ 1 row in set (0.00 sec)
We can use _ wildcard character one or more than one times also. Here’s an example:
mysql> SELECT 'Tutorialmines' LIKE 'Tut_____mines'; +--------------------------------------+ | 'Tutorialmines' LIKE 'Tut_____mines' | +--------------------------------------+ | 1 | +--------------------------------------+ 1 row in set (0.00 sec)
Below example, Using both wild characters i.e. _ and % .
mysql> SELECT 'Tutorialmines' LIKE 'Tut_rial%'; +----------------------------------+ | 'Tutorialmines' LIKE 'Tut_rial%' | +----------------------------------+ | 1 | +----------------------------------+ 1 row in set (0.00 sec)
The LIKE operator is often used within a WHERE clause of a SELECT statement. Below is an example of how we can use this operator within a database query. Below query will fetch all rows which has name starting with charater “p”.
mysql> SELECT name, age from tbl_employee WHERE name LIKE 'p%'; +-------+-----+ | name | age | +-------+-----+ | Polla | 30 | +-------+-----+ 1 row in set (0.00 sec)
To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the ESCAPE character, \ is assumed.
What happens if one of the wildcard characters are in your input string and you need to perform a match against it? You can escape it with the backslash character (\).
Lets see the below example.
mysql> SELECT 'Tutorial_mines' LIKE 'Tutorial_mines' AS 'With-out-escape', 'Tutorial_mines' LIKE 'Tutorial\_mines' AS 'With-escape'; +-----------------+-------------+ | With-out-escape | With-escape | +-----------------+-------------+ | 1 | 1 | +-----------------+-------------+ 1 row in set (0.00 sec))
In above case, they both matched, but for different reasons.
Let’s change the input string slightly so that we get a different result:
mysql> SELECT 'Tutorial_mines' LIKE 'Tutorial_mines' AS 'With-out-escape', -> 'Tutorial+mines' LIKE 'Tutorial\_mines' AS 'With-escape'; +-----------------+-------------+ | With-out-escape | With-escape | +-----------------+-------------+ | 1 | 0 | +-----------------+-------------+ 1 row in set (0.00 sec)
To specify a different escape character, use the ESCAPE clause. See below example
mysql> SELECT 'Tutorial_mines' LIKE 'Tutorial|_mines' ESCAPE '|' AS Result, 'Tutorial+mines' LIKE 'Tutorial|_mines' ESCAPE '|' AS Result1; +--------+---------+ | Result | Result1 | +--------+---------+ | 1 | 0 | +--------+---------+ 1 row in set (0.00 sec)
The escape sequence should be empty or one character long. The expression must evaluate as a constant at execution time. If the NO_BACKSLASH_ESCAPES SQL mode is enabled, the sequence cannot be empty.
MySQL permits LIKE on numeric expressions also. See below example
mysql> SELECT 9872 LIKE '9%', 9872 LIKE '98_'; +----------------+-----------------+ | 9872 LIKE '9%' | 9872 LIKE '98_' | +----------------+-----------------+ | 1 | 0 | +----------------+-----------------+ 1 row in set (0.00 sec)
In above case, 9872 match the given pattern,So it returns 1. In second part 9872 LIKE ’98_’, given pattern is set to match 3numbers, while in expression 4numbers are there. so, it returns 0.
String comparisons are not case-sensitive unless one of the operands is case-sensitive (uses a case-sensitive collation or is a binary string). See below example
mysql> SELECT 'tuts' LIKE 'TUTS'; +--------------------+ | 'tuts' LIKE 'TUTS' | +--------------------+ | 1 | +--------------------+ 1 row in set (0.00 sec) mysql> SELECT 'tuts' LIKE _latin1 'TUTS' COLLATE latin1_general_cs; +------------------------------------------------------+ | 'tuts' LIKE _latin1 'TUTS' COLLATE latin1_general_cs | +------------------------------------------------------+ | 0 | +------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT 'tuts' LIKE _latin1 'TUTS' COLLATE latin1_bin; +-----------------------------------------------+ | 'tuts' LIKE _latin1 'TUTS' COLLATE latin1_bin | +-----------------------------------------------+ | 0 | +-----------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT 'tuts' LIKE BINARY 'TUTS'; +---------------------------+ | 'tuts' LIKE BINARY 'TUTS' | +---------------------------+ | 0 | +---------------------------+ 1 row in set (0.00 sec)
If either expression or patstring is NULL, it will return NULL. See below example
mysql> SELECT 9872 LIKE NULL, NULL LIKE '98_'; +----------------+-----------------+ | 9872 LIKE NULL | NULL LIKE '98_' | +----------------+-----------------+ | NULL | NULL | +----------------+-----------------+ 1 row in set (0.00 sec)
See all MySQL String functions MySQL 8 String Functions.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…