MySQL LIKE and Wildcards
LIKE searches for a pattern in text. Two wildcards make the pattern.
| Wildcard | Meaning |
|---|---|
% | Any number of characters (even none) |
_ | Exactly one character |
| Pattern | Matches |
|---|---|
'A%' | Starts with A |
'%a' | Ends with a |
'%an%' | Contains “an” anywhere |
'_o%' | Second letter is o |
SELECT name FROM students WHERE name LIKE 'A%';
Result
| name |
|---|
| Asha |
SELECT name FROM students WHERE name LIKE '%a';
Result
| name |
|---|
| Maria |
| Asha |
| Neha |
SELECT name FROM students WHERE name LIKE '_o%';
Result
| name |
|---|
| Robin |
| John |
SELECT name FROM students WHERE name LIKE '%i%';
Result
| name |
|---|
| Robin |
| Maria |
| Vikram |
Read the interview question names that begin with S.
NOT LIKE
SELECT name FROM students WHERE name NOT LIKE '%a%';
Result
| name |
|---|
| Robin |
| John |
Note:
LIKE is not case-sensitive with the default settings. A search like %word with a starting % cannot use an index, so it can be slow on big tables. For big text search use a FULLTEXT index.
Summary
%is any text,_is one character.- Use
NOT LIKEfor the opposite. - Next: IN, BETWEEN and IS NULL.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |