How to return the expected result in this query?

Asked

Viewed 38 times

2

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

Currently she is finding 5 results, which means that the query is pulling the blogs of the category "feed" 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'
                               ");

1 answer

5


Your current condition can be divided into these two parts:

1: Hacker And Automobiles
OR
2: Food


Actually, what you want is this:

1: Hacker
And
2: Automobiles OR Food

So, you need to parentheses in the second group so that they are solved as one condition, thus staying:

$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' )
                           ");
  • Waouuuuwwww and I thought I was terrible at programming. rsrsrs Thanks Bacco

Browser other questions tagged

You are not signed in. Login or sign up in order to post.