Search code examples
phpmysqlsql-like

WHERE condition with Multiple LIKE referring to multiple rows


I have an html/php input that gets me $_POST['search_query'].

I would like to filter results by post title or by tags. Whatever is simillar should appear in the results table.

I was trying this query:

SELECT * FROM posts WHERE activado='1' AND (titulo LIKE '%".$_POST['search_query']."%')
                    OR (tag1 LIKE '%".$_POST['search_query']."%') 
                    OR (tag2 LIKE '%".$_POST['search_query']."%') 
                    OR (tag3 LIKE '%".$_POST['search_query']."%') 

It's filtering the "titulo" row only, I would need to fetch the simillar tags row too.

I want that the output brings any simillar Rows being fetched if the row is Titulo, Tag1, Tag2 or Tag3. At the momment is only fetching titulo rows with simillar content.

What am I missing here? I bet its a really simple change.


Solution

  • Please try this:

    SELECT * FROM posts
    WHERE activado='1'
    AND ((titulo LIKE '%".$_POST['search_query']."%')
    OR (tag1 LIKE '%".$_POST['search_query']."%')
    OR (tag2 LIKE '%".$_POST['search_query']."%')
    OR (tag3 LIKE '%".$_POST['search_query']."%') );