KDEx
KDEx

Reputation: 3657

SQL syntax errors

I am running my first SQL query and not sure where my error lies. The first line (for the date) seems to run correctly. None of the other lines seem to be working IE it returns a results with ratings of less than 3.5. I am not receiving an error message when I run the query, it runs successfully. If possible explain the 'concept' that i'm missing.

It seems like this should be a simple query, it pulls from one table 'app'. 'rating' 'downloads' and 'lastUpdate' are all columns.

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
downloads = '1 - 5' OR '10 - 50' OR '50 - 100' OR '100 - 500'

Upvotes: 1

Views: 76

Answers (3)

JohnFx
JohnFx

Reputation: 34909

You can't string together ORs like that. Try this alternate version:

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 

(downloads = '1 - 5' OR 
 downloads ='10 - 50' OR
 downloads ='50 - 100' OR 
 downloads ='100 - 500')

You can shorten this syntax a bit using the IN clause as follows.

   downloads IN('1-5','10-50','90-100','100-500')

This assumes that downloads is a varchar or text type containing values like '1-5' and that it isn't a numeric and you are expecting this to return 1,2,3,4,5.

If you mean the latter you need to specify those parameters like this

(downloads between 1 and 5 OR 
 downloads between 10 and 50 OR
 downloads between 50 and 100 OR 
 downloads between 100 and 500)

Upvotes: 3

Johnnyoh
Johnnyoh

Reputation: 369

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
(downloads between 1 AND 5 OR downloads between 10 AND 50 OR downloads between 50 AND 100 OR downloads between 100 AND 500)

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26068

Try this

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
(downloads IN ('1 - 5','10 - 50','50 - 100','100 - 500'))

I'm not sure your syntax was correct to string or values together, and if it is you need parenthesis or any of those or conditions will return true for the whole query.

Upvotes: 4

Related Questions