Wednesday, April 15, 2009

MySQL: BETWEEN

It's often necessary when constructing queries to limit a result set based on a particular field falling within a specified range. For example, if you want to select records from a table that fall within a certain date range you may write a query as follows:
SELECT * FROM sometable
WHERE date <= '2009-04-15'
AND date >= '2009-04-01'

An alternative way to construct the above query is by using the BETWEEN statement:
SELECT * FROM sometable
WHERE date BETWEEN '2009-04-01' AND '2009-04-15'

2 comments:

  1. I am totally gonna use the bejeezus out of this construct from now on.

    ReplyDelete
  2. which query is faster both query...

    ReplyDelete