For a MySQL select where a string contains a word, there is no CONTAINS function. The answer is LIKE with a percent sign on both sides: WHERE column LIKE '%word%'. Most mistakes come after that first query, when you add a second word, need the match to ignore or respect upper case, or look for a literal %. This article covers each case with a small table you can create yourself, and every result shown comes from a real MySQL server.

MySQL where contains: query string contains with examples
Every example below uses this table, which has the same columns the first version of this article used from a WordPress posts table. Run it in the mysql client to follow along:
CREATE TABLE posts (
id INT PRIMARY KEY AUTO_INCREMENT,
post_name VARCHAR(100),
post_title VARCHAR(200),
post_content TEXT,
KEY idx_title (post_title),
FULLTEXT KEY ft_content (post_content)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO posts (post_name, post_title, post_content) VALUES
('send-email-mailkit', 'Send email with MailKit', 'Use MailKit to send mail over SMTP from .NET.'),
('smtp-basics', 'SMTP basics', 'SMTP is the protocol used to send mail.'),
('mailkit-imap', 'Read mail with IMAP', 'MAILKIT can also read mail over IMAP.'),
('discount', 'Sales report', 'We gave a 50% discount this quarter.'),
('snake', 'Naming style', 'Use snake_case for column names.'),
('plain', 'Plain post', 'Nothing relevant here.');
Query pattern
The percent sign matches any number of characters, including none. Put it on both sides of the word to match it anywhere in the text. This is the pattern, with your own column, table and word:
SELECT column_a
FROM table_o
WHERE column_b LIKE '%search_word%';
Note the old version of this pattern had a stray comma after COLUMN-A, which is a syntax error. The query above has none. The LIKE operator is described in the MySQL 8.0 Reference Manual: String Comparison Functions.
Example
SELECT post_name, post_title
FROM posts
WHERE post_content LIKE '%mailkit%';
Result:
+--------------------+-------------------------+
| post_name | post_title |
+--------------------+-------------------------+
| send-email-mailkit | Send email with MailKit |
| mailkit-imap | Read mail with IMAP |
+--------------------+-------------------------+
Both rows match, including MAILKIT in capitals. That is not an accident; see the case section below.
Rows that do not contain a word
Use NOT LIKE. On the sample table it returned smtp-basics, discount, snake and plain:
SELECT post_name FROM posts WHERE post_content NOT LIKE '%mailkit%';
Be aware that rows where the column is NULL do not match NOT LIKE either, because the comparison result is NULL, not true. Add OR post_content IS NULL if you want them.
MySQL string contains multiple words
Write the full LIKE condition for every word and join them with OR (either word) or AND (both words):
-- contains mailkit OR smtp
SELECT post_name FROM posts
WHERE post_content LIKE '%mailkit%' OR post_content LIKE '%smtp%';
-- contains mailkit AND smtp
SELECT post_name FROM posts
WHERE post_content LIKE '%mailkit%' AND post_content LIKE '%smtp%';
On the sample table the first query returned send-email-mailkit, smtp-basics and mailkit-imap. The second returned only send-email-mailkit, the one row that has both words.
The old example was wrong
The first version of this article wrote the second word without its own column and LIKE, as LIKE '%mailkit%' OR 'smtp'. MySQL accepts that without an error, which hides the mistake. It treats 'smtp' as a number, which becomes 0, so the second half is always false. I ran it:
SELECT post_name FROM posts WHERE post_content LIKE '%mailkit%' OR 'smtp';
-- send-email-mailkit, mailkit-imap (smtp-basics is missing)
-- Warning 1292: Truncated incorrect DOUBLE value: 'smtp'
The row smtp-basics is missing, so the query did not search for smtp at all. Always repeat the column and LIKE for each word. For many words, a regular expression is shorter:
SELECT post_name FROM posts WHERE post_content REGEXP 'mailkit|smtp';
That returned the same three rows as the OR query. The syntax is in the MySQL 8.0 Reference Manual: Regular Expressions.
Case-sensitive and case-insensitive contains
LIKE follows the collation of the column. The table above uses the MySQL default for utf8mb4, utf8mb4_0900_ai_ci, which ignores case and accents. That is why LIKE '%MAILKIT%' found both rows. To require the exact case, use LIKE BINARY or a case-sensitive collation:
SELECT post_name FROM posts WHERE post_content LIKE BINARY '%MailKit%';
-- send-email-mailkit
SELECT post_name FROM posts WHERE post_content COLLATE utf8mb4_0900_as_cs LIKE '%MAILKIT%';
-- mailkit-imap
The first matched only the row written MailKit. The second, searching for MAILKIT, matched only the row written in capitals. If your column or database uses a different collation, the default behavior changes with it. The comparison rules are in the MySQL 8.0 Reference Manual: String Comparison Functions.
Search for a literal % or underscore
In a LIKE pattern, % means any text and _ means exactly one character. To find the characters themselves, escape them with a backslash, or choose your own escape character:
SELECT post_name FROM posts WHERE post_content LIKE '%50\%%'; -- discount
SELECT post_name FROM posts WHERE post_content LIKE '%snake\_case%'; -- snake
SELECT post_name FROM posts WHERE post_content LIKE '%snake!_case%' ESCAPE '!'; -- snake
Each returned its single expected row. An unescaped snake_case also matched here, but only because the underscore stands for any one character and the text happens to contain an underscore, so it would also match snakeXcase.
LOCATE, INSTR and FULLTEXT
LOCATE and INSTR return the position of a word, so > 0 means contains. They need no escaping, which makes them handy when the search text comes from a variable or contains wildcards:
SELECT post_name FROM posts WHERE LOCATE('mailkit', post_content) > 0;
SELECT post_name FROM posts WHERE INSTR(post_content, 'smtp') > 0;
The first returned the two mailkit rows, the second send-email-mailkit and smtp-basics. For word search in large text columns, a FULLTEXT index with MATCH ... AGAINST is built for the job (MySQL 8.0 Reference Manual: Full-Text Search Functions):
SELECT post_name FROM posts
WHERE MATCH(post_content) AGAINST ('mailkit' IN NATURAL LANGUAGE MODE);
SELECT post_name FROM posts
WHERE MATCH(post_content) AGAINST ('+mailkit +smtp' IN BOOLEAN MODE);
The natural language query returned both mailkit rows, and the boolean query with a plus sign on each word (both words required) returned only send-email-mailkit. FULLTEXT matches whole words, not parts of words, and ignores very short and very common words by default, so it is not a drop-in replacement for LIKE '%part%'.
Searching with a variable
Build the pattern with CONCAT and pass the search text as a parameter, never by gluing it into the SQL string from user input:
SET @term = 'smtp';
PREPARE s FROM 'SELECT post_name FROM posts WHERE post_content LIKE CONCAT(''%'', ?, ''%'')';
EXECUTE s USING @term;
-- send-email-mailkit, smtp-basics
In application code, use your driver’s parameters in the same way. For connecting from .NET, see use .NET C# to connect to a MySQL database.
Why a leading % is slow
A pattern that starts with a wildcard cannot use a normal B-tree index. I ran EXPLAIN on the indexed post_title column:
EXPLAIN SELECT post_name FROM posts WHERE post_title LIKE 'SMTP%';
-- Index range scan on posts using idx_title
EXPLAIN SELECT post_name FROM posts WHERE post_title LIKE '%SMTP%';
-- Table scan on posts
The prefix search used the index. The contains search scanned the table. On six rows that does not matter, but on millions it does. If you search the start of a value, drop the leading %. If you need contains on a large table, use FULLTEXT or a search engine. More production guidance is in MySQL best practices for production. To page through the matches, see pagination in SQL.
What was tested and what was not
- Run on macOS against a local MySQL server (version 26.10.0 from the Homebrew mysql package), default utf8mb4_0900_ai_ci collation: every query above on the six-row sample table. The result lines are real output. The reference links point to the MySQL 8.0 manual hosted by Oracle, since the MySQL site rejects automated link checks.
- Not tested: MariaDB, MySQL 5.7, other collations, large tables and timings (the
EXPLAINoutput only shows which access path was chosen), and FULLTEXT stopword and minimum word length settings.
Summary
- Contains is
column LIKE '%word%'. Repeat the full condition for each word and join withORorAND. - Case follows the collation. Use
LIKE BINARYfor exact case, and escape%and_when you mean the characters. - A leading
%scans the table. Use FULLTEXT for large text columns.
Found this useful?
Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.
MySQL string contains FAQ
How do I write a MySQL SELECT where a column contains a string?
There is no CONTAINS function. Use WHERE column LIKE '%word%', or LIKE with percent signs, LOCATE or INSTR for a position test, REGEXP for patterns, or MATCH … AGAINST with a FULLTEXT index for word search.
Is MySQL LIKE case-sensitive?
It depends on the collation. With the default utf8mb4_0900_ai_ci it ignores case. Use LIKE BINARY or a case-sensitive collation such as utf8mb4_0900_as_cs to match exact case.
How do I check that a column contains several words?
Write one LIKE condition per word and join them with AND for all words or OR for any word. Writing LIKE ‘%a%’ OR ‘b’ is a mistake: the second operand is not compared with the column.
Which query did you need? Tell us in the comments if a case is missing.
