Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.19     Range matching: between() and in()

This is NOT the latest copy of this book; click here for the latest version.

Although there is a lot you can do with standard operators like <, >, <=, etc, they are clunky to work with when you want to specify an exact range of items you want to check for. For example, the query to return all people with an age between 16 and 21 or 25 and 29 looks like this using the standard operators:

SELECT * FROM people WHERE (Age >= 16 AND Age <= 21) OR (Age >= 25 AND Age <= 29);

Using the between() function you can specify the low- and high-point a little more easily, giving the following query:

SELECT * FROM people WHERE Age BETWEEN(16,21) OR Age BETWEEN(25,29);

The two queries are functionally the same (between(16,21) matches 16, 17, 18, 19, 20, and 21; it is inclusive), and there is not really any big size difference between the two. However, the second query is much easier to read, as I think you will agree, because of the lack of symbols.

The other important function available here is in(), which allows you to specify the exact range of possibilities that will be matched against. Using in() to specify a range of constants is very, very fast, particularly if you specify a range of integers. Here are a couple of examples:

SELECT * FROM people WHERE Age IN (19, 20, 21);
SELECT * FROM people WHERE FirstName IN ('Sam', 'Bill', 'Patricia');

Using in() is a great way to avoid having to use multiple ORs in your WHERE clause.





<< 9.3.18 Advanced text searching using full-text indexes   9.3.20 NULL >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 01 Dec 2008

I'd like to verify that I couldn't get the BETWEEN() function to work either, but I was able to use BETWEEN value AND value instead. I am using MySql verson 4.1

A PHP User - 01 Dec 2008

In the mysql 4.0.22 version this "between" syntax doesn`t works, I think

I think exists an error in the "between" syntax, reviewing the manual I found that function doesn`t work with braces and it need the key word "and" in the middle operands. that is the above example will be:

SELECT * FROM people WHERE Age BETWEEN 16 and 21 OR Age BETWEEN 25 and 29 ;

at least in the 4.0.22 mysql version the above example doesn`t works.

A PHP User - 01 Dec 2008

In the mysql 4.0.22 version this "between" syntax doesn`t works, I think

I think exists an error in the "between" syntax, reviewing the manual I found that function doesn`t work with braces and it need the key word "and" in the middle operands. that is the above example will be:

SELECT * FROM people WHERE Age BETWEEN 16 and 21 OR Age BETWEEN 25 and 29 ;

at least in the 4.0.22 mysql version the above example doesn`t works.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow