This one has me pretty confused and after reading numerous forums and "how to" sites, I think I'm even more confused why this isn't working.
I have 2 tables that I am joining.
Table 1 (products) contains all my product details.
Table 2 (ratings) contains all the ratings for each product and each product has 3 ratings from 3 different rating entities.
I am working on a set of AJAX search filters and need to write the query in a manner that incorporates all of the filters.
The filters include price, number of reviews, average review stars, rating 1, rating 2, rating 3.
The price, num reviews, and avg stars comes from Table 1 (products).
Rating 1, 2, and 3 come from Table 2 (ratings).
I need something like this for my query, but this is returning zero results when I know (have verified in the database) that I have records that meet the criteria I'm querying for.
SELECT p.id, p.name
FROM products p
JOIN ratings r ON p.id = r.id
WHERE p.num_reviews BETWEEN '0' AND '1000'
AND p.price BETWEEN '0' AND '500'
AND p.avg_rvw_stars BETWEEN '0' AND '5'
AND (r.ratingSource='1' AND (r.rating BETWEEN '1' AND '4'))
AND (r.ratingSource='2' AND (r.rating BETWEEN '0' AND '100'))
AND (r.ratingSource='3' AND (r.rating BETWEEN '0' AND '100'))
ORDER BY p.name ASC
The issue seems to reside in the last three WHERE statements. If I remove any 2 of the 3, I get results. But when I include all 3, I get nothing when there are records that qualify.
If I change the last two AND's to OR, I also get results, but it's not the result set that is expected or needed.
Am I not writing this correctly?
Any help/guidance is greatly appreciated!!