Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:14+00:00 2026-06-10T06:30:14+00:00

This one has me pretty confused and after reading numerous forums and how to

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:30:15+00:00Added an answer on June 10, 2026 at 6:30 am

    If you have 3 different rows with the ratings, you can’t use that logic. Each row is checked for the WHERE conditions once, so there cannot be a row with a rating both 1 and 2.

    The easiest way is to join thrice:

    SELECT p.id, p.name 
    FROM products p 
      JOIN ratings r1 ON  p.id = r1.id 
                      AND r1.ratingSource = 1
                      AND r1.rating BETWEEN 1 AND 4
      JOIN ratings r2 ON  p.id = r2.id 
                      AND r2.ratingSource = 2
                      AND r2.rating BETWEEN 0 AND 100
      JOIN ratings r3 ON  p.id = r3.id
                      AND r3.ratingSource = 3
                      AND r3.rating BETWEEN 0 AND 100
    WHERE p.num_reviews BETWEEN 0 AND 1000 
      AND p.price BETWEEN 0 AND 500 
      AND p.avg_rvw_stars BETWEEN 0 AND 5 
    ORDER BY p.name ASC ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has me pretty rattled so I thank you in advance for your
I've been pretty good at fixing my Xcode Obj-C errors, but this one has
I'm pretty new to Java and this has me really confused. I have two
Usually I feel pretty confident with SQL queries, however this one has me scratching
This one has been stumping me for a while. But I'm no expert. This
This one has me scratching my head, so I'm hoping a second pair of
This one has proven to be a little tricky for me so far. I
This one has me stumped. <div id=container> <div id=store></div> <div id=contact></div> </div> I'm looking
This one has me stumped. Here are the relative bits of code: public AgencyDetails(Guid
This one has made me a lot of stress over the last couple of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.