How to return the expected result in this query?

2

Good afternoon, guys. The case is as follows: I have a table with 2 blogs of category "automovel" and part of the title "Hackers" then whatever my query, can not return more than two results combining the "title" and "category" . Can you help me solve this query to get the desired result?

Currently she is finding 5 results, which means that the query is pulling blogs from the "feed" category by ignoring the combination with the title.

    $result = $this->db->query("
                               SELECT * FROM blogs AS bl
                               INNER JOIN blog_categoria AS blc
                               ON bl.bc_id = blc.id
                               WHERE bl.bl_title
                               LIKE '%Hackers%'
                               AND blc.bc_urllink = 'automoveis'
                               OR blc.bc_urllink = 'alimentacao'
                               ");
    
asked by anonymous 19.11.2016 / 15:26

1 answer

5

Your current condition can be divided into these two parts:

  

1: Hacker E Cars
OU
    2: Food


Actually, what you want is this:

  

1: Hacker
E
    2: Cars OU Food

So, you need parentheses in the second group so that they are resolved as a single condition, so:

$result = $this->db->query("
                           SELECT * FROM blogs AS bl
                           INNER JOIN blog_categoria AS blc
                           ON bl.bc_id = blc.id
                           WHERE bl.bl_title
                           LIKE '%Hackers%'
                           AND ( blc.bc_urllink = 'automoveis'
                           OR blc.bc_urllink = 'alimentacao' )
                           ");
    
19.11.2016 / 15:31