0
SELECT * 
FROM [productDetail] 
WHERE 
     Price BETWEEN 0 AND 2000 
     OR Price BETWEEN 2000 AND 12000 
     AND CategoryID = 2  
     AND caravanOffline = 0 
ORDER BY 
     price

The query doesn't produce the desired output, it also shows the products whose categoryID is 3,1 etc.

Can any one spot the problem in the query.

Updated code:

static public DataTable GetSelectedFilterMotorhomes(ArrayList caravans)
{

    string sqldefault = "Select * from productDetail Where";
    string sqlBuilder = "";
    int starter = 0;
    int count = caravans.Count;
    string sqlOperator = "OR";

    if (caravans.Count > 0)
    {

        while (count > starter) //build the query depending on the number of caravans.selected.count 
        {

            sqlBuilder += "((Price between " + caravans[count - 1] + "))" + sqlOperator;
            count--;


        }

        string addQuery = sqldefault + sqlBuilder;
        string removeOR = addQuery.Substring(0, addQuery.Length - 2);  //remove OR
        string finalQuery = removeOR + "AND (CategoryID = 2)  AND (caravanOffline = 0)  order by Price"; //Display caravans in ascending order

        SqlDataAdapter da = new SqlDataAdapter(finalQuery, ConnectionString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
    else
    {
        string returnAllCaravans = "Select * from productDetail WHERE CategoryID = 2 AND caravanOffline = 0";

        SqlDataAdapter da = new SqlDataAdapter(returnAllCaravans, ConnectionString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

}
4
  • 1
    It would be helpful if you could let us know what you are trying to achieve with the WHERE clause, it's rather vague. Commented May 31, 2011 at 11:03
  • You still haven't told us what you're trying to achieve. Commented May 31, 2011 at 11:12
  • Actually , i have got the checklist box on the website , query builts on the number of check box user checks, there is a price category which have different range for instance in the above example i have just given two ranges Commented May 31, 2011 at 11:13
  • 1
    It's still not clear what you actually want, can't you just explain what you want the query to bring back? Commented May 31, 2011 at 11:16

3 Answers 3

3

You may need some parenthesis? I'd need to know exactly what you were trying to pull back with your query but likely you need a parenthesis after the OR and then again either after the CategoryID = 2 or after the caravanOffline = 0. See if either of the following queries returns what you're looking for.

Select
    *
from [productDetail]
WHERE
    Price between 0 AND 2000
    OR (Price between 2000 AND 12000 AND CategoryID = 2) AND caravanOffline = 0
order by price

Select
    *
from [productDetail]
WHERE
    Price between 0 AND 2000
    OR (Price between 2000 AND 12000 AND CategoryID = 2 AND caravanOffline = 0)
order by price
Sign up to request clarification or add additional context in comments.

1 Comment

They're not, the first one always uses the caravanOffline = 0 restriction, whereas the last one only applies it to caravans in the 2000 to 12000 price range with a category of 2.
1

What do you really want to do with your WHERE clause?? Without any parenthesis, it's not clear at all what you're trying to select

DO you really mean:

WHERE 
     (Price BETWEEN 0 AND 2000 OR Price BETWEEN 2000 AND 12000)
     AND CategoryID = 2  
     AND caravanOffline = 0 

Price is between 0 and 2000, or between 2000 and 12000 (doesn't really make sense), AND in any case, the CategoryID is 2 ?

Or do you mean:

WHERE 
     Price BETWEEN 0 AND 2000 
     OR 
     (Price BETWEEN 2000 AND 12000 AND CategoryID = 2 AND caravanOffline = 0)

The price is either between 0 and 2000 (with no other restriction) OR the price is between 2000 and 12000, but only if the CategoryID = 2 and CaravanOffline = 0 are true at the same time) ??

Or are you looking for something else than these two options??

2 Comments

Actually , i have got the checklist box on the website , query builts on the number of check box user checks, there is a price category which have different range for instance in the above example i have just given two ranges
@Muhammad Awais: you need to explain (in plain English) what you're trying to do - not just post more code which still doesn't make it clear what your intention is....
1

I think it might need some brackets...

SELECT * FROM productDetail
WHERE Price BETWEEN 0 AND 2000 
OR (Price BETWEEN 2000 AND 12000
AND CategoryId = 2
AND caravanOffline = 0)
ORDER BY price

Assuming that's the logic you want! Your question is rather vague, I'm assuming that you want caravans under 2000 regardless of category and caravanOffline, or caravans between 2000 and 12000 with a categoryid of 2 and caravanOffline of 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.