I'm building a date specific PHP script.
This script will perform a specific task when its Sunday. I've written my PHP code and I'm trying to test it. Since it's not Sunday today, I set my system date to last Sunday. The thing is that my MySQL query doesn't return the expected results
SELECT date FROM schedules WHERE name='someName' AND date >= (CURDATE() - INTERVAL 3 DAY )
The query above will return the records of the current date plus and minus 3 days.
Here is an example of the output I get:
2015-11-26
2015-11-29
2015-11-30
2015-12-04
2015-12-02
The result that I need is:
2015-11-26
2015-11-29
Another query:
SELECT date FROM schedules WHERE name='someName' AND date >= (CURDATE() - 3 )
The query above will return all the dates that are 3 days older than the current date not including the last 3 days of the current date.
I only want it to return the last 3 days of the current date.
I'm trying to understand the output you want. So you want to get dates that are no more than 3 days prior to the current date and nothing in the future.
So on '2015-12-04' the result would be:
2015-12-04
2015-12-03
2015-12-02
2015-12-01
So maybe the BETWEEN Clause will do the trick
SELECT date FROM schedules
WHERE name='someName'
AND (date BETWEEN (CURDATE() - INTERVAL 3 DAY ) AND CURDATE() );
Related
I want to get all records from the last x months from my MySQL server. Using 2 months for example(Not from last 2 months, like last 60 days, but from the entire past month and so on. If actual month is april, I want all records from february and march).
I've tried some queries and the last one is
SELECT id FROM ranking WHERE (end_date BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH), '%Y-%m-%d'))
"end_date" is my date column which is a DATE column "2017-04-07".
That query above just returns nothing and I cant figure out where is the error.
Try this:
SELECT id
FROM ranking
WHERE end_date BETWEEN
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 3 MONTH)) INTERVAL 1 DAY)
AND LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH));
It calculates dates as like this:
For start date in range, it subtracts 3 months from current date, gets the last day of that month and adds a day in it to get the first day of next month (1st February in our case)
For end date, it subtracts one month from current date and gets the last day of that month (31st March in our case)
Try without convert it to string, And BETWEEN requires min date first.
SELECT id
FROM ranking
WHERE end_date BETWEEN LAST_DAY(CAST(NOW() as date) - INTERVAL 2 MONTH
AND CAST(NOW() as date)
I have two columns(date field, time field) which are storing date and time in string and first step is to combine these two fields so i can compare them with current date(i'm not sure if i'm doing that right). Second step is writing query which should select records only if obtained date from these two columns is close to current date. And that date should be between current date and 4 hours before current date because after that something will happen with all those records if some conditions are not met.
Format of columns date and time:
event_date = 'yyyy/MM/dd'
event_time = 'HH:mm'
So here is my query:
SELECT *
FROM `events`
WHERE DATE_SUB( NOW( ) , INTERVAL 4 HOUR )
< ADDTIME( event_date, event_time )
Maybe i should firstly convert these strings to date and then work with them or something else? All in all if i have some data with these dates for example:
1. 2017-02-26 10:00
2. 2017-02-26 11:00
3. 2017-02-27 10:00
And current datetime is: 2017-02-26 06:00
. In this case i would get only 1 record and when one hour pass or 2017-02-26 07:00 then i will get 2 records
If event_date is the date of the event and event_time is the time, you could combine them using the timestamp mysql function:
select event_date, event_time, timestamp(event_date, event_time)
from events;
but it's usually a good idea to actually store the date and time information in a single field e.g. event_datetime
you can then write your query like this:
select *
from
events
where
event_datetime between now() and now() + interval 4 hour;
this will return all events starting from now and 4 hours in the future. Or if you want the events from the last 4 hour in the past try with:
event_datetime between now() - interval 4 hour and now();
You should convert your date and time to a proper datetime data type. That said, you can do something like the following to accomplish what you want:
SELECT * FROM `events`
WHERE STR_TO_DATE(CONCAT(event_date,' ',event_time), '%Y/%m/%d %H:%i')
BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 4 HOUR)
The query will return all rows between now and 4 hours from now.
UPDATE: I just noticed the answer from #fthiella, using timestamp probably gives better performance than the example I provided.
I am trying to display only records two weeks in advance from the current date onwards. starttime is stored as a datetime data type in the database.
I have this,
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start, date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates WHERE user_id = 1 AND `status`='' AND YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0),
YEARWEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK), 0))
But I am getting a syntax error YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0) AND YEARWEEK(DATE_ADD(NOW()
Could anyone tell me what's wrong with it?
It seems MySQL does not support calculated column in the where clause:
try
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start,
date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates
WHERE user_id = 1 AND `status`='' AND
YEARWEEK(starttime, 0) IN (
YEARWEEK(DATE_ADD(NOW(), INTERVAL 1 WEEK), 0),
YEARWEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK), 0))
use starttime instead of formatted_date
As I said, I see no reason to use YEARWEEK function. You need start date set to today which is CURDATE() and plus 2 weeks period which is DATE_ADD(CURDATE(), INTERVAL 2 WEEK) and then we just check if starttime between those 2 dates.
SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start,
date_format(starttime, '%Y-%m-%d') AS formatted_date,
date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates
WHERE user_id = 1
AND `status`=''
AND DATE(starttime) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 2 WEEK)
How do you define "two weeks in advance"? Our normal inclination, is to think "within two weeks", which would be to simply before 14 days from now.
WHERE starttime >= NOW()
AND starttime < NOW() + INTERVAL 14 DAY
But the original query is using YEARWEEK function, and the behavior with that is a bit different.
If today is Thursday, May 21, 1015. What is the range of dates that starttime should fall into?
For datetimes right now or in the future, we can just do:
WHERE starttime >= NOW()
(We can do just a > rather than >= if we want to exclude startime values that are an exact match for NOW().
The question is, the bit about "two weeks in advance". Adding 14 days, or 2 weeks, would get us up to Thursday, June 4, 2015. It looks like you might also want to include Friday and Saturday, June 5th and 6th. (Up until Sunday, June th.)
We can use an expression to return "Sunday on or following 14 days from today"
If it's a Sunday, we just use that date. (For convenience, we think of that as adding 0 days). If it's Saturday, we need to add 1 day. If it's a Friday, add 2 days, ... Monday, add 6 days.
Conveniently, the expression 6 - WEEKDAY(NOW()) gives us the number of days we need to add to today's date to get to the next Sunday.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday
We probably want to lop the time off at midnight, we can use the DATE() function to do that.
-- startime values on or after now and before midnight
-- of the first sunday on or after the date two weeks from now
WHERE starttime >= NOW()
AND starttime < DATE(NOW()) + INTERVAL 14+6-WEEKDAY(NOW()) DAY
It's much easier to read and understand what the query is doing when we reference bare columns from the table on one side, and do the gyration and manipulation on the other side. It's also easier to test. And, maybe more importantly, it allows MySQL to make effective use of an "range scan" operation on a suitable index, rather than having to evaluate a function on every row in the table.
If today is '2015-05-21', and what you meant by "two weeks in advance" was up until the second Sunday from now (Sunday, May 31, 2015... 1 week and 3 days from now) just replace the 14 with 7.
Again, it really depends on how you define "two weeks in advance", how you determine what that ending date boundary is. Once you can define that, you can write an expression that returns the value, and just compare to value stored in the starttime column.
I've got a syntax problem I can't sort out. I'm just trying to grab all records from last 3 days.
$result = mysqli_query($link,"SELECT * FROM records WHERE today BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY)");
today is DB column for the MySQL timestamp and looks like this: 2014-10-30 16:35:58
This query only gives results for 1 day, not 3. Can someone help with the syntax problem?
DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY) means three days in the future, not three days ago. Unless the today column is supposed to represent (say) the date for which a future appointment is scheduled, you usually want to subtract days from a date. So get three days ago, you need to use DATE_SUB. I'd recommend this query:
SELECT *
FROM records
WHERE today >= DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY)
I am retrieving data from a table and show the total SUM of entries. What I want to do is to show the total SUM of entries made on today's date, yesterday and this month. The table is using the unix timestamp format (e.g. 1351771856 for example).
Currently I am using this line to show todays results:
AND comment_date > UNIX_TIMESTAMP() - 24 * 3600";
but that gives me just the entries for the last 24 hours.
Example: So let's say its Friday, 17:00 PM - it gives me the count from Thursday 17:00 PM to Friday 17:00 PM
What I want is to get the results for
Thursday 00:00:00 - 23:59:59 (yesterday in this case)
the results for today (00:00:00 - 23:59:59)
and last week, results that start on Monday, 00:00:00 until "today" (in this case Friday).
I couldn't find a way in the MySQL documentation to achieve this.
This mysql code should work for you:
// Today
AND DATE(from_unixtime(comment_date)) = CURRENT_DATE
// Yesterday
AND DATE(from_unixtime(comment_date)) = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY)
// This week
AND YEARWEEK(from_unixtime(comment_date), 1) = YEARWEEK(CURRENT_DATE, 1)
// This month
AND YEAR(from_unixtime(comment_date)) = YEAR(CURRENT_DATE)
AND MONTH(from_unixtime(comment_date)) = MONTH(CURRENT_DATE)
Simply use this:
AND comment_date > date_sub(current_date, interval 1 day)
See my answer here, I think it's quite related.
Pull records from orders table for the current week
Consider getting intimate with MySQL's GROUP BY. You will most likely need to know this if you use MySQL.