MySQL query for rows where date is in this week - mysql

I'm trying to query for all rows where an event has been completed THIS week, where weeks start on Sunday. I've tried this query, but it doesn't seem to work (I've tried both CURDATE() and NOW(), not sure if that matters), I get no rows returned:
SELECT `TodoCompleted`.`day`, `TodoCompleted`.`date` FROM `todo_completed` AS
`TodoCompleted` WHERE `TodoCompleted`.`employee_id` = 'whatever' AND
`TodoCompleted`.`completed` = 0 AND WEEK(`TodoCompleted`.`date`) = 'WEEK(NOW())'
I've also tried this, but it isn't what I'm looking for since it pulls records from the previous week:
AND `TodoCompleted`.`date` BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()
I can't just get them all within an interval of a week, I need just THIS week (Sunday - Saturday).
Any help would be much appreciated!

Use YEARWEEK()
AND YEARWEEK(`TodoCompleted`.`date`) = YEARWEEK(CURDATE())
and remove the quotes around it.

Related

MYSQL WHERE clause, last week year over week

I have a portion of a WHERE clause below and I though I had this right but I dont seem to:
AND (WEEK(DeliveryDate, 1) = (WEEK(CURDATE(), 0) - 1))
AND (YEAR(DeliveryDate) = YEAR(CURDATE())))
The way I though this worked according to the documention was that the "1" here (WEEK(DeliveryDate, 1)' was telling it to start the week on Monday. Then the "0" here = (WEEK(CURDATE(), 0) - 1)) was telling it to start in the week that just ended. This is however not what is happening. Instead this expression is calculating the week the ended November 29th instead of this past week that ended December 6th. What is being missed?
EDIT 1
The above WHERE clause is what I was told to move to at one point at the suggestion of another user. I had previously been using CURDATE() then the Interval between X and Y to get one week ago, not including this week and not the last 7 days. Here is the result of the folling query:
select post_id,DeliveryDate from table where deliverydate >= now() - interval 1 week
If you are trying to see if DeliveryDate is in the previous week, where weeks start on Monday, do:
AND YEARWEEK(DeliveryDate, 1) = YEARWEEK(CURDATE() - INTERVAL 1 WEEK, 1)
Adjusting the return of week won't work well across year boundaries, and checking week and year separately can go wrong in a number of ways, and comparing week values using different modes isn't going to do anything useful.

Date Intervals Doesn't work

I am trying to get data by week, month and year.
I store date YYYY-MM-DD HH:MM:SS.
What I am doing is below;
Fetch one week old data;
query + AND WEEK(date) = WEEK(CURDATE())
Fetch a month old data;
query + AND MONTH(date) = MONTH(CURDATE())
The thing is I couldnt be able to get the data correct. For instance when I want to get week old data, I am gettin a year old one too.
Is there any other query that I could use? I have tried DATE(NOW()) - INTERVAL 30 DAY. It works but very slow.
Thanks!
I believe that the problem is that the WEEK function returns the week of the year. So, Jan 1st 2017 might be week 1 (also might be week 53 of the previous year depending on the day of the week and how MySQL handles it). But then, Jan 1st of 2016 is also week 1 - just for a different year.
Trying changing it to:
query + AND WEEK(date) = WEEK(CURDATE()) AND YEAR(date) = YEAR(CURDATE())
Also, if you're storing this as a string then definitely change it to a DATETIME
WHERE ...
AND date >= CURDATE() - INTERVAL 7 DAY
AND date < CURDATE()
Gives you the 7 days ending with yesterday. Use other techniques to get a particular month or week.
This technique is also much faster for large tables with a suitable index. Hiding date inside a function, such as WEEK() prevents the use of an index.

MySQL Select From Prior Week, not just 7 days

I am attempting to get a query that selects the week prior (Sun-Sat). I've fought with this query and the closest I can get is the last 7 days, using the following:
SELECT *
FROM dates
WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE();
I'm really unsure how to proceed from here. It seems as if I need to create some kind of relation between CURDATE() and the Saturday before maybe?
Any help is appreciated.
You are after the Week of the year.
Look at the Week Function: WEEK(date[,mode])
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_week
The mode describes how you define the week (which is the start of the week)
WEEK(date,3) is for a week that starts Monday.
SELECT *
FROM dates
WHERE
-- Last Week
WEEK(date,3) = WEEK(CURDATE(),3)-1
AND YEAR(date)= YEAR(CURDATE()) ;
Don't forget the year. Week is just a number between 1 and 52. So the Year is important!
The code above is not correct. it will fail on the last week of the year!

Rewritten mySQL Statement

SELECT DATE(STR_TO_DATE(CONCAT(CONCAT(YEAR('$uDate1'), week), ' Monday'), '%X%V %W') +
INTERVAL (7 - DAYOFWEEK(STR_TO_DATE(CONCAT(CONCAT(YEAR('$uDate1'), week), ' Monday'),
'%X%V %W'))) DAY)
as week_end_date
What this statement does is take the date I give it ($uDate1) and give me the week end date (Saturday) of that week. This works well and I am happy with it, kinda.
I was wondering if there were some things I missed that would either make this more efficient or even if I missed some shortcuts to this.
Any suggestions for me?
week >= WEEK('$uDate1') AND week <= WEEK('$uDate2')
This is in my WHERE clause. So basically if I use this...
DATE('$uDate1', INTERVAL 7 - DAYOFWEEK('$uDate1') DAY)
...then it returns the same day for all records. I need it to be able to go over a span of a few weeks.
I have a column in my database named 'week'. It simply stores an INT that corresponds to the week of the year. (ex. 21 for this week)
I then have two date picker boxes. The output gets the week end date based of each week that is BETWEEN and INCLUDES the days chosen.
5/10/2016 & 5/26/2016 outputs 5/14/2016, 5/21/2016, 5/28/2016
What gets exported to CSV file looks something like this..
WEEK END, LAST NAME, FIRST NAME, ...
5/10/2016, Smith, John, ...
5/26/2016, Jones, James, ...
It outputs anyone who had hours during the week, with the week end date.
SIDE NOTE: I do appreciate the comments and help. I don't want anyone to stress over this though! Just curious if better way. :)
I am not sure why your current SQL is so complicated.
You say it is just to take a date and give me the week end date (Saturday) of that week .
How you are doing this at the moment is:-
Yours is taking the year
Adding the week of the year (I assume - should be WEEK('$uDate1') I think)
Adding on the day as a string (so for example for today it would be 2016 21 Monday )
Changing that string back to a date a datetime value
Converting that datetime value back to a date.
Then taking the year again
Adding the week of the year again
Getting the day of the week of the resulting string. As you have concatenated Monday on to the date then the day of the week will always be 2.
Taking that resulting day of the week and subtracting it from 7. As the day of the week will always be 2 this will always result in 5
Adding on the day as a string (so for example for today it would be 2016 21 Monday ).
This value is then added on to the previously calculated date, taking the Monday date and adding 5 days.
My suggestion was to just use:-
DATE_ADD($uDate1, INTERVAL 7 - DAYOFWEEK($uDate1) DAY)
which is far simpler, and appears to cover your requirements.
EDIT
Looking at your edit you want a list of all the Saturdays for weeks all or partially between 2 passed dates.
If so I think the following will do it and hopefully be more efficient as there is no need to translate dates to and from string. Note it relies on your week table to add to the date, hence only copes with date ranges of up to that many weeks.
SELECT DATE_ADD(DATE_ADD('$uDate1', INTERVAL 7 - DAYOFWEEK('$uDate1') DAY), INTERVAL `week` WEEK) AS aDate
FROM `week`
HAVING aDate BETWEEN '$uDate1' AND DATE_ADD('$uDate2', INTERVAL 7 - DAYOFWEEK('$uDate2') DAY)
ORDER BY aDate
As I mentioned in comment you should move this transformation from mysql query to php code.
I see no reason to do this calculation on mysql side.
http://ideone.com/48zLvF
$week_day = intval(date('w',$uDate1));
if ($week_day<6) {
$end_of_week = $uDate1+(86400*(6-$week_day));
} else {
$end_of_week = $uDate1;
}

Trying to select the number of mysql rows inserted yesterday

I can normally do this but it appears my brain is not functioning well right now and I'm missing something.
Every day via a cron job that is run at 1am, I want to get a count of rows that were inserted yesterday, and the date.
ie
SELECT DATE(added) as date, COUNT(*) FROM bookings WHERE added = DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY date
'added' contains a timestamp ie '2011-04-18 12:31:31'
What am I getting wrong here? I know there are many rows added yesterday but my query is returning 0 results and no mysql_errors :(
Any ideas?
Please try
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY yesterday
or perhaps
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) GROUP BY yesterday
Updated
Corrected the WHERE part.
well whatever NOW() is will return the time portion and unless they were added at exactly that time the day before they wont be counted.
So either use BETWEEN and specify time range, or format the date in your query to only match on the day month year components and not time
WHERE added = does only match exact NOW() - 1 DAY, you should select by a range instead.