-1

i'm try to get data last by 30 days, and then i'm order this by hit because i want to get last 30 days data trending.

SELECT *, 
       category.title AS nama_kategori 
FROM   category 
       join posting 
         ON category.alias = posting.id_category 
WHERE  DATE BETWEEN Now() - interval 30 day AND Now() 
       AND id_category LIKE '%gga%' 
        OR id_category LIKE '%wstb%' 
ORDER  BY posting.hit DESC 

the output of this query is there the data is not las by 30 days ( since 1 oct ) there the other data that have other years. enter image description here

sumit
  • 15,003
  • 12
  • 69
  • 110
Didik Ariyana
  • 39
  • 3
  • 8
  • Possible duplicate of [SQL Logic Operator Precedence: And and Or](https://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or) – Nathan Hughes Oct 01 '19 at 02:53

1 Answers1

1

This is because of your OR query id_category LIKE '%wstb%' which will return all entries with that category regardless of your date query .

Add parenthesis on AND query and pass multiple OR conditions

SELECT *, 
       category.title AS nama_kategori 
FROM   category 
       join posting 
         ON category.alias = posting.id_category 
WHERE  DATE BETWEEN Now() - interval 30 day AND Now() 
       AND (id_category LIKE '%gga%' 
        OR id_category LIKE '%wstb%') 
ORDER  BY posting.hit DESC 
sumit
  • 15,003
  • 12
  • 69
  • 110