Philip Kirkbride
Philip Kirkbride

Reputation: 22879

Query items from a database between dates

I'm using PHP and SQL to create a stats summary based on weekly periods.

I initially thought all the items would have the same date and the code below worked in that situation.

$result1 = mysql_query("SELECT * FROM phoneappdetail WHERE salebarn = 'OSI' AND saledate = '2012-06-6' ORDER BY wtcode ");

However now I've found out that the items I need to query have varying days.

How can I change my query to get dates between 2012-06-6 and 2012-06-12 instead of only dates exactly on 2012-06-6?

Upvotes: 0

Views: 68

Answers (3)

Brandon Kreisel
Brandon Kreisel

Reputation: 1636

$result1 = mysql_query("SELECT * FROM phoneappdetail WHERE salebarn = 'OSI' AND saledate > '2012-06-6' AND saledate < '2012-06-12' ORDER BY wtcode ");

Upvotes: 0

Randy
Randy

Reputation: 16677

saledate between '2012-06-6' and '2012-06-12'

Upvotes: 0

Zbigniew
Zbigniew

Reputation: 27594

Use SQL BETWEEN clause:

SELECT * FROM phoneappdetail WHERE salebarn = 'OSI' AND (saledate BETWEEN '2012-06-6' AND '2012-06-12') ORDER BY wtcode 

Upvotes: 3

Related Questions