I have articles stored in a MySQL table. Now I want to display related articles. So I integrated a field called "tags", which stores all tags separated by commas. Until now I search for common tags like this:
foreach (explode(',', $article['tags']) as $key => $value) {
if ($key != 0)
$search.= ' OR';
$search .= " `tags` LIKE '%" . mysql_real_escape_string($value) . "%'";
}
$query = "SELECT title, id FROM `article` WHERE $search";
That created a query like this:
SELECT title, id FROM `article` WHERE `tags` LIKE '%tag1%' OR `tags` LIKE '%tag2%'
I am wondering if there is a more efficient way to solve this. The negative side of my current solution is the "%" which causes that extreme short Tags like "green" collide with "greenteam" .