Skip to main content

like

like

Description

Syntax:

BOOLEAN like(VARCHAR str, VARCHAR pattern)

This function performs fuzzy matching on the string str. If the string matches the condition, it will return true; if it doesn't, it will return false.

The like match/fuzzy match will be used in combination with % and _.

% represents zero, one, or more characters.

_ represents a single character.

'a'   // Precise matching, has the same effect as "="
'%a' // data ending with "a"
'a%' // data starting with "a"
'%a%' // data containing "a"
'_a_' // three-digit data with "a" as the middle character
'_a' // two-digit data with "a" as the last character
'a_' // two-digit data with "a" as the first character
'a__b' // four-digit data with "a" as the first character and "b" as the last character

Example

// table test
+-------+
| k1 |
+-------+
| b |
| bb |
| bab |
| a |
+-------+

// Return the data containing "a" in the k1 string
mysql> select k1 from test where k1 like '%a%';
+-------+
| k1 |
+-------+
| a |
| bab |
+-------+

// Return the data equal to "a" in the k1 string
mysql> select k1 from test where k1 like 'a';
+-------+
| k1 |
+-------+
| a |
+-------+

Keywords

LIKE