我有一个网上商店的搜索引擎,我想让用户有机会通过评级来过滤产品,直到现在我的查询看起来是这样的:
SELECT products.*,prices.price,AVG(ratings_products.rating)
FROM products,prices,ratings_products
WHERE products.title REGEXP 'lg' AND products.active = 'yes'
AND prices.price >= '10' AND prices.price <= '298650'
AND products.cod_product = prices.id_product
AND ratings_products.ratings >= '40' AND ratings_products.ratings <= 59
AND ratings_products.id_product = products.cod_product
AND products.id_category = '59' LIMIT 0,1
这个想法是按照价格、特定的过滤器和评级进行过滤,除了评级之外,其他都有效。我有如下结构的ratings_products表:
id | id_product | id_user | title | comment | date | rating
1 | 18 | 65 |awesome| some |12344 | 85
2 | 18 | 84 |cool | great |12345 | 20
现在我需要AVG来返回85到20之间的平均值,但查询返回NULL,如果我删除ratings_products.rating <= 59
,它会给我结果,但不会计算平均值。
感谢的任何帮助
感谢
AND ratings_products.ratings >= '40' AND ratings_products.ratings <= 59
这意味着您需要介于40和59之间的值。您在示例中显示20和85,两者都不在您查询的范围内,因此结果为NULL
删除AND ratings_products.ratings <= 59
部分会留下一个高于40(即85)的评级查询,其本身没有任何平均值。
是我遗漏了什么,还是你在查询错误的值?