Home >Database >Mysql Tutorial >How to Find SQL Rows Containing Specific Words?

How to Find SQL Rows Containing Specific Words?

Linda Hamilton
Linda HamiltonOriginal
2025-01-15 17:47:45129browse

How to Find SQL Rows Containing Specific Words?

Find rows whose fields contain specific words in SQL

Question:

You need a SQL query that returns all rows in a table where a specified field contains one or more words from a given list. The words can appear in the field in any order.

Solution:

To retrieve rows where a field contains any specified word, use the LIKE operator with wildcard characters:

<code class="language-sql">SELECT *
FROM MyTable
WHERE Column1 LIKE '%word1%'
   OR Column1 LIKE '%word2%'
   OR Column1 LIKE '%word3%'</code>

To retrieve rows where a field contains all the specified words, use the AND condition:

<code class="language-sql">SELECT *
FROM MyTable
WHERE Column1 LIKE '%word1%'
  AND Column1 LIKE '%word2%'
  AND Column1 LIKE '%word3%'</code>

Note:

To improve performance when searching for multiple words, consider using full-text search, which is supported by most major databases. The specific implementation of full-text search depends on the database type.

The above is the detailed content of How to Find SQL Rows Containing Specific Words?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn