Last Updated:

MySQL :  Search for a word or string

MySQL
 

Task: You need to find in which fields of the table the requested word or row occurs and query the data of all the fields of the tables that satisfy the query.

The solution to this task will be a SQL query containing the LIKE statement.

Let me give you a primitive example.
Let's say we need to


display all the rows in the text field that contain the word "articles".

Write a query:

Query logic: get data from all fields in the post table, where the word "articles" appears in the text field.


 
SELECT * FROM post WHERE text LIKE '%articles%'

/* Affected rows: 0 Lines found: 2 Warnings: 0 Duration 1 query: 0,000 sec. */


Query to find a word at the beginning of a row:

Query logic: Get data from all fields in the post table, where the word "articles" appears at the beginning of the text field.


Query to find a word at the end of a row:

Query logic: Get data from all fields in the post table, where the word "articles" appears at the end of the text field.
SELECT * FROM post WHERE text LIKE 'articles%'

SELECT * FROM post WHERE text LIKE '%articles'

The % sign inside the condition of the LIKE operator indicates that there may be some text before or after the word "articles" required for searching.