Forum Moderators: coopster
I can't figure out and find much information on making Mysql queries for the following:
Retrieve all rows for the date range of the current week. ex. Since it is Thursday today, I want to know what rows have been created since Sunday of this week.
Basically what I am trying to do is create a reports page for how many orders was made:
- today
- yesterday
- current week
- last week
- last month
Right now i have it like this and I'm not sure if this is correct or not.
$today = mysql_num_rows(mysql_query("SELECT * FROM Orders WHERE ODate = CURDATE()"));
$yesterday = mysql_num_rows(mysql_query("SELECT * FROM Orders WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= ODate"));
$this_week =
$last_week =
$last_month = mysql_num_rows(mysql_query("SELECT * FROM Orders WHERE DATE_SUB(CURDATE(),INTERVAL 1 MONTH) <= ODate"));
Any help would be great..thanks
--
-- current week (starting with Sunday)
--
SELECT
COUNT(*) AS rows
FROM Orders
WHERE YEARWEEK(ODate) = YEARWEEK(CURRENT_DATE)
;
--
-- last week (starting with Sunday)
--
SELECT
COUNT(*) AS rows
FROM Orders
WHERE YEARWEEK(ODate) = YEARWEEK(CURRENT_DATE - INTERVAL 7 DAY)
;
--
-- last month
--
SELECT
COUNT(*) AS rows
FROM Orders
WHERE
SUBSTRING(ODate FROM 1 FOR 7) =
SUBSTRING(CURRENT_DATE - INTERVAL 1 MONTH FROM 1 FOR 7)
;
But as for:
$last_month = mysql_num_rows(mysql_query("SELECT * FROM Orders WHERE DATE_SUB(CURDATE(),INTERVAL 1 MONTH) <= ODate"));
I don't understand FROM AND FOR syntax used here.
SUBSTRING(str,pos)
SUBSTRING(str FROM pos)
SUBSTRING(str,pos,len)
SUBSTRING(str FROM pos FOR len)The forms without a
lenargument return a substring from stringstrstarting at positionpos. The forms with alenargument return a substringlencharacters long from stringstr, starting at positionpos. The forms that useFROMare standard SQL syntax. It is also possible to use a negative value forpos. In this case, the beginning of the substring isposcharacters from the end of the string, rather than the beginning. A negative value may be used forposin any of the forms of this function.
Or are you asking about what the substring is returning? Well, let's say your date was '2006-07-10'. Then the WHERE clause would be using only the first 7 positions, or '2006-06', the year and month portion of the dates being compared. Cool, eh?