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

How to Find SQL Rows Containing Specific Words?

Linda Hamilton
Release: 2025-01-15 17:47:45
Original
127 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template