0

Is there a faster way to get database results from rows that match a series of numbers?

For example, I have this...

$result = mysql_query("SELECT * FROM scores WHERE p_cat=10 OR p_cat=11 OR p_cat=12 OR p_cat=13 OR p_cat=14 OR p_cat=15 OR p_cat=16 OR p_cat=17 OR p_cat=18 OR p_cat=19 ORDER BY p_time ASC LIMIT 50")

so basically I'd like it to match row that match any number 10 through 19. But all of these ORs seem so redundant and daunting... especially when I have to do this for ranges of tens up to 200.

2 Answers 2

3

Use BETWEEN:

WHERE p_cat BETWEEN 10 AND 19

If it's not a consecutive sequence, use IN:

WHERE p_cat IN (10, 12, 13, 15, 19)
Sign up to request clarification or add additional context in comments.

2 Comments

is that actually any faster? Or just more concise?
I think it's likely to be faster, especially if there's an index on the column.
0

use this code to fetch the desired result WHERE p_cat BETWEEN 10 AND 1

           $result = mysql_query("SELECT * FROM scores
               WHERE p_cat>=10 AND p_cat<=19   
              ORDER BY p_time ASC LIMIT 50")

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.