Select dates within the last week, but not only last 7 days - mysql

I want to select all dates within the last week.
But not by simply counting the current date - 7, as all posts Ive come across suggest.
This is how I actually have it for the SUM now....
SELECT SUM(total) FROM payforms WHERE user_id = 1 GROUP BY WEEK(date)
This gives me a nice total...
But I want to retrieve all individual records within the last week.
So I can use a BETWEEN query....but how do I get it to look in the current week.
Example...
Tuesday I want it to only find values from Sun, Mon, Tue.
On wednesday, I want it to find Sun, Mon, Tue, Wed.
On Saturday it finds the whole previous week. etc
So to be more clear....
I dont want it to find last Monday, on a monday.
On Mondays it should only display Mondays, if you get what I mean.
Can I do this??
Thanks

select * from payforms where yearweek(date) = yearweek(now());
Although I think MySQL weeks start on Sunday

You can make use of the WEEKDAY date function, which returns the day index of the week.

If I understand your question correctly, you could use this query:
SELECT date, SUM(total)
FROM payforms
WHERE date >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY
GROUP BY date
Please see fiddle here.
This will SUM all totals for every day of the current week, starting on last Sunday. If you want it to start on last Monday you can use this:
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY

Related

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;
}

Selecting mysql records for next week

I'm looking for the MySQL query to select date records that fall within the next week using Sunday - Saturday as the format for the week.
So in other words, I'm not looking to get the dates a week from today, I'm looking to get the dates that fall within Sunday - Saturday of the next week.
I found this: MySQL Query to select data from last week? and it works for the previous week from Sunday - Saturday but I'm not sure how to tweak this to get the dates for the next week.
Any ideas?
SELECT * FROM table
WHERE
date > date_add(curdate(),INTERVAL(7-dayofweek(curdate()))DAY)
AND date <= date_add(curdate(),INTERVAL(14- dayofweek(curdate()))DAY)
You can use:
SELECT *
FROM YourTable
WHERE WEEK(YourDateField, 6) = WEEK(CURRENT_DATE + INTERVAL 7 DAY, 6)
for selecting recods of next week (starting with sunday)
Weeks count is 1 to 53 with first week of the year having at least 4 days in the year

Getting the last month's dates in mysql

I need to get the last month's dates from 1st to current date. Suppose if today's date is March 25th, I need to get the dates from 1st to 25th of february. Suppose if today's date is March 30th, I need to get the dates from 1st to 28/29th Feb, whatever the maximum final date is available. I have searched a lot to get that, but no luck. Can someone please help me how to get this special case done? I am able to do it on another database, but I want to do this on mysql.
Basically what I did for other database is this --> date between date(to_char(date(add_months(DATE(sysdate) ,-1)),'YYYY-MM-01 00:00:00')) and date(add_months(DATE(sysdate) ,-1))
This should do what you want:
WHERE d BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m-01')
AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
You can use DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)). This will automatically limit the result to the last day of the month, so if today's date is March 30th, this will return Feb 28th.

Get data up until last day of previous month in MySQL

I've got an mysql-db with charges in it, with a datetime. Every month i want to create an invoice with all the charges from last month or earlier. So if it´s may 2nd, 5th or 30th 2012, i still only want the invoices from april 2012 or earlier. I've tried with date_sub, but it just subtracts a month, so it only invoices up to the same day of the previous month. How should i do this?
get * from Ads WHERE AdEnd > ??
Ty!
I always found that if you subtract the day of the month in days from the current date, you'll get the last day of the previous month. For example, on the 15th of the month, subtract 15 days, and you'll end up with the last day of the previous month:
SELECT (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY))
If CURDATE() is 2012-05-05 then the above returns 2012-04-30.
So, to get Ads up to the last day of last month, do something like this:
SELECT *
FROM Ads
WHERE AdEnd <= (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY))
This works if AdEnd is a DATE, but if it's a DATETIME, you'll just want to do less than the first of the month, so you subtract one less day to get the first of the month like this:
SELECT *
FROM Ads
WHERE AdEnd < (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) - 1 DAY))
Try date_diff, for example:
SELECT DATEDIFF('2008-11-30','2008-11-29') AS DiffDate
yields:
1