我尝试获取过去30天的数据,然后按命中排序,因为我想获取过去30天的数据趋势。
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
此查询的输出是,那里的数据不是在30天之前(自10月1日起),而其他数据还有其他年份。 enter image description here
答案 0 :(得分:1)
这是因为您的OR查询id_category LIKE '%wstb%'
会返回与该类别无关的所有条目,而与您的日期查询无关。
在AND查询上添加括号并传递多个OR条件
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