I am writing a php script that takes in a search form separated by spaces (example: 'purple mustang') and I would like to search a table in my database, let's call it articles
.
Articles has 2 fields, title
and tags
. The title
is something simple like "purple mustang for sale" and the tags
are a comma separated string like "purple, car, mustang, sale, new"
I would like to pull out the information of the database by matching the searched terms to both the title
and the comma separated tags
, but prioritizing matches to the title
to come before matches to tags
. (I hope that makes sense)
So with this example, I want to pull out anything that matches the terms purple mustang in the title
, and then below that (for anything not matching in the title) anything matching the tags
.
Right now I am doing a simple SELECT
from the table articles
but I'm not sure how to go about using the LIKE to search multiple keywords, like 'purple mustang'.
Any help here would be appreciated. I have used lots of MYSQL before but never tried to match multiple keywords to multiple fields before.
SELECT *
FROM articles
WHERE
title LIKE '%mustang%'
AND
title LIKE '%purple%'