How to Match String Patterns with LIKE in MariaDB
MariaDB’s LIKE tests whether a whole string matches a pattern. WHERE code LIKE '123%' finds values starting with 123, while LIKE '%34%' finds values containing 34 anywhere. % matches zero or more characters, and _ matches exactly one.
Prepare the sample rows in one connection
Select a practice database and create the temporary table below. VARCHAR lets you match patterns in codes that look numeric and keeps leading zeros intact.
CREATE TEMPORARY TABLE like_demo (
id INT PRIMARY KEY,
code VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
INDEX idx_like_code (code)
);
INSERT INTO like_demo (id, code) VALUES
(1, '12345'), (2, '23456'), (3, '123'),
(4, 'AB_12'), (5, 'ABX12'), (6, 'AB%12'),
(7, 'AB12'), (8, 'ab-9'), (9, NULL);
Keep using the connection that created the temporary table. Open a new connection to start over. The practice account needs permission to create temporary tables.
Match a prefix, suffix, or substring
In a LIKE pattern, % can represent an empty string or any number of characters, and _ represents one character regardless of how many bytes it uses. Use = for equality of the full value under the applicable collation. Place the wildcards as shown to match only a prefix, a suffix, or a substring.
| Condition | Meaning | Matching sample values |
|---|---|---|
= '1234' |
Entire value equals 1234 | No matches |
LIKE '123%' |
Starts with 123 | 12345, 123 |
LIKE '%345' |
Ends with 345 | 12345 |
LIKE '%34%' |
Contains 34 anywhere | 12345, 23456 |
LIKE '%23%5' |
Contains 23 and ends with 5 | 12345 |
LIKE 'AB_12' |
One character between AB and 12 | AB_12, ABX12, AB%12 |
SELECT code FROM like_demo WHERE code = '1234' ORDER BY id;
SELECT code FROM like_demo WHERE code LIKE '123%' ORDER BY id;
SELECT code FROM like_demo WHERE code LIKE '%345' ORDER BY id;
SELECT code FROM like_demo WHERE code LIKE '%34%' ORDER BY id;
SELECT code FROM like_demo WHERE code LIKE '%23%5' ORDER BY id;
SELECT code FROM like_demo WHERE code LIKE 'AB_12' ORDER BY id;
'123%' matches both 12345 and 123 because % can match an empty string. 'AB_12' needs exactly one character in the middle, so it excludes AB12.
Search for literal percent signs and underscores
To match the wildcard symbols themselves, choose a single escape character with ESCAPE and put it before the literal % or _. This example chooses !.
SELECT code FROM like_demo
WHERE code LIKE 'AB!_12' ESCAPE '!';
SELECT code FROM like_demo
WHERE code LIKE 'AB!%12' ESCAPE '!';
The first query returns only AB_12, and the second returns only AB%12. With this escape character, !! represents a literal exclamation mark. When building a pattern from user input, escape the escape character first, then the wildcards, and bind the result as a query parameter.
Control case sensitivity
Case sensitivity follows the string’s collation rules. The example column uses utf8mb4_general_ci, which ignores case. To make only this query case-sensitive, use COLLATE with utf8mb4_bin, which is compatible with the column’s character set.
SELECT code FROM like_demo
WHERE code LIKE 'ab%'
ORDER BY id;
SELECT code FROM like_demo
WHERE code COLLATE utf8mb4_bin LIKE 'ab%'
ORDER BY id;
The first query returns AB_12, ABX12, AB%12, AB12, and ab-9. The second returns only ab-9. Choose a collation compatible with the character set of your actual column.
Handle NULL with NOT LIKE
NOT LIKE finds values that fail the pattern. If the value or pattern is NULL, both LIKE and NOT LIKE return NULL. WHERE keeps only rows for which its final condition is true.
SELECT id, code FROM like_demo
WHERE code NOT LIKE '123%'
ORDER BY id;
SELECT id, code FROM like_demo
WHERE code NOT LIKE '123%' OR code IS NULL
ORDER BY id;
The first query returns IDs 2, 4, 5, 6, 7, 8. Add OR code IS NULL, as in the second query, when missing codes should be included. ID 9 then appears.
Check index range access
An indexed string column can use index range access for a fixed prefix such as '123%'. A leading wildcard in '%123%' prevents that kind of range lookup. Pick the pattern that matches what you need to find, then use EXPLAIN to inspect the plan.
EXPLAIN SELECT code FROM like_demo WHERE code LIKE '123%';
EXPLAIN SELECT code FROM like_demo WHERE code LIKE '%123%';
The example’s prefix search shows type = range with key = idx_like_code. The substring search shows type = index, meaning it scans the full index. Plans depend on the indexes and data distribution, so inspect type, key, and rows for your own query.



