w3resource

MySQL Full-Text Search on Multiple Columns with Solution


Full-Text Search on Multiple Columns

Write a MySQL query to search for the term "tutorial" in both the "Title" and "Content" columns of the Articles table.

Solution:

-- Search for the term 'tutorial' in both the Title and Content columns.

-- Select all columns from the Articles table where the Title or Content columns match the search term.
SELECT * 
-- Specify the table from which to retrieve the data.
FROM Articles

-- Use the MATCH...AGAINST clause to perform a full-text search on multiple columns.
-- MATCH(Title, Content) specifies the columns to search, and AGAINST('tutorial') specifies the search term.
WHERE MATCH(Title, Content) AGAINST('tutorial');

Explanation:

  • Purpose of the Query:
    • To locate articles where either the title or content includes the word "tutorial".
    • Demonstrates how to search across multiple columns using full-text search.
  • Key Components:
    • MATCH(Title, Content) : Specifies multiple columns for the search.
    • AGAINST('tutorial') : Provides the search term.
  • Real-World Application:
    • Enhances search functionality in content management systems by broadening the search scope.

Notes:

  • A full-text index must exist on both the Title and Content columns.
  • Results are ranked by relevance based on the search term's occurrence.

For more Practice: Solve these Related Problems:

  • Write a MySQL query to perform a full-text search for "user guide" across both the "Title" and "Content" columns, combining their relevance scores.
  • Write a MySQL query to search for the term "tutorial" in both the "Title" and "Content" columns, but exclude rows where the word "draft" appears.
  • Write a MySQL query to perform a weighted full-text search on the "Title" and "Content" columns, giving higher priority to matches in the "Title".
  • Write a MySQL query to search for articles where either "Title" or "Content" contains "introduction" and return only rows with a combined relevance above a specified threshold.

Go to:


PREV : Basic Full-Text Search on a Single Column.
NEXT : Boolean Mode Full-Text Search.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.