So, I can do the following to get data from last week.
select * from table where week(date)=week(curdate())-1
Same for 2 weeks ago. But this fails if the data is in the prior year. What query can I use to get data from n weeks ago regardless of what year the data belongs to.
Edit: The week starts on Sunday 12AM and ends Saturday 11:59PM
When does a "week" start? Sunday? Monday? The same day of week as today?
Assuming you are happy with the last option, do this:
SELECT ...
WHERE date >= CURDATE() - INTERVAL $n WEEK
AND date < CURDATE() - INTERVAL $n-1 WEEK
Example
mysql> SELECT CURDATE(), CURDATE() - INTERVAL 9 WEEK, CURDATE() - INTERVAL 9-1 WEEK;
+------------+-----------------------------+-------------------------------+
| CURDATE() | CURDATE() - INTERVAL 9 WEEK | CURDATE() - INTERVAL 9-1 WEEK |
+------------+-----------------------------+-------------------------------+
| 2015-02-22 | 2014-12-21 | 2014-12-28 |
+------------+-----------------------------+-------------------------------+
If you need the week to start on a particular DOW, the query is messier, by further subtracting INTERVAL DAYOFWEEK(CURDATE()) DAY. And that could be off a little.
The start of the current week (assuming it is Sunday) is CURDATE() - INTERVAL (WEEKDAY(CURDATE() + INTERVAL 1 DAY)). So, replace CURDATE() in the above expression (twice) with this long mess.
Related
i need to do a statistics and get results starting tuesday midnight to next week monday midnight
if today is tuesday then i get results only for today and every next day will show results from tuesday to current day.
something like
select * from my_table where day_of_week > tuesday and day_of_week < monday
what i allready tried :
SELECT * FROM `mytable` where userId = 123 and date >= curdate() - INTERVAL DAYOFWEEK(curdate()) - 3 DAY AND date <= curdate() - INTERVAL DAYOFWEEK(curdate()) - 7 DAY
thanks
How to select all data from last month (or 30 days)?
I already found some answers, and mostly gives this solution
SELECT *
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC
But this gives me also the dates from the future
I am only interested in the days from last month or 30 days (not next month and beyond)
Is this what you want?
WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE
I added a condition so the query filters on date not greater than today. I also modified your code so the date range starts one month ago (you had 3 months).
try this code
SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC
You are asking for two separate things.
The last 30 days is easy.
date between now() - interval 30 day and now()
Data this month is like this:
date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())
Data a few months ago is like this:
date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY)
and
(last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 DAY)
I'm doing a review of existing code and have found the following SQL query which is used to get a selection of records last month.
Is there a more concise way of writing SQL to do what this date based clause does in MySQL?
SELECT foo
FROM some_table
WHERE some_date
BETWEEN
DATE_FORMAT(LAST_DAY((NOW() - INTERVAL 1 MONTH) - INTERVAL 1 SECOND), '%Y-%m-01 00:00:00')
AND
DATE_FORMAT(LAST_DAY((NOW() - INTERVAL 1 MONTH) - INTERVAL 1 SECOND), '%Y-%m-%d 23:59:59')
It works, but I just twitch a little every time I see it.
Can anyone else write it better?
Thank you in advance.
There's no need to format the dates, they default to YYYY-MM-DD 00:00:00.
This is a little bit simpler:
SELECT foo
FROM some_table
WHERE some_date >= LAST_DAY(CURDATE() - INTERVAL 2 MONTH) + INTERVAL 1 DAY
AND some_date < LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
So if CURDATE() is today, 2019-02-06, then:
- INTERVAL 2 MONTH is 2018-12-06
LAST_DAY() of that date is 2018-12-31
+ INTERVAL 1 DAY is 2019-01-01
Then the upper bound is:
- INTERVAL 1 MONTH is 2019-1-06
LAST_DAY() of that date is 2019-1-31
+ INTERVAL 1 DAY is 2019-02-01
The dates should be strictly less than 2019-02-01.
Using less than accounts for timestamps in the last second of the month, between 23:59:59.000 and 23:59:59.999.
user | completed
mike | 2016-07-10 19:00:00
john | 2016-07-11 08:00:00
I am trying to select all rows in a database where the row completed is NOT between 14:00 the previous day and the current day before 10:00. The script is designed to be run at 10:30 everyday
I've tried this
SELECT name FROM daily_tracking WHERE completed NOT BETWEEN now() - interval 1 day AND NOW() - INTERVAL 8 hour
you should use date_sub
SELECT name
FROM daily_tracking
WHERE completed NOT BETWEEN date_sub(NOW(), interval 1 day )
AND date_sub(NOW(), INTERVAL 8 hour)
I would not depend on the exact time when the script is being run. Instead, use arithmetic based on the current date:
SELECT dt.name
FROM daily_tracking dt
WHERE completed < date_sub(curdate(), interval (24 - 14) hour) or
completed > date_add(curdate(), interval 10 hour);
This will work on a given day, regardless of the time the script is run.
You can also write it this way, which I prefer...
SELECT dt.name
FROM daily_tracking dt
WHERE dt.completed BETWEEN CURDATE() - INTERVAL 14 HOUR AND CURDATE() + INTERVAL 10 HOUR;
How do I SELECT the first week of a previous month I've tried
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>=(CURDATE()- 1 WEEK - INTERVAL 2 week)";
This was used to try and select the third week but this didn't work
Does this work for you:
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>= curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week
AND startDate < curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week _+ interval 1 week";
?
The idea here is that we first go back a month, then find the start of the week (assuming Monday, for Sunday we will need some extra tweaking), then figure out how many whole weeks it has been from the start of the month, and subtract that many weeks from the date so far. This takes us back to the start of the first week of the month. For the end of the range we just add one week to the start.
Ah, your question became more detailed.
Not really familiar with sql, there might be better but something like:
SELECT repairId , startDate,catId, statusId FROM repair
WHERE EXTRACT(YEAR_MONTH FROM start_date) = EXTRACT(YEAR_MONTH FROM NOW()) - 1 AND CAST(EXTRACT(DAY FROM start_date) / 7 + 1 as INT) = ?;
Basically, extract the year month components to compare year and month and then extract the day of month use the flooring caused by integer truncation to get the week to compare with whatever week you are looking for
mysql return rows matching year month