Hi I am comparing two dates using mysql between function. My query looks like
select count(id) as total
from table
where user_id=111
and date_column BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY)
and NOW()
In between part of this query it is BETWEEN upperdate and lowerdate. It is working fine. But I went to verify this function on mysql documentation https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between . It says it should be
`BETWEEN LOWER AND UPPER`
Current I am doing in reverse and it is working fine but I just want to verify that is it rite and it will cause in problem in future and any hidden case.
It is working. Because:
DATE_SUB(NOW(), INTERVAL 1 DAY) -- a day less than now is yesterday
is LOWER THAN
NOW() -- it is now, means today with current time
So comparison would be like
between yes'day and today
which is valid
BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) and NOW()
is in other words
BETWEEN yesterday AND today
So it is
BETWEEN lower AND upper
No wonder it's working :)
Related
For some reason, I cannot wrap my head around the syntax and logic for this problem.
My most recent iteration of the code looks like this.
SELECT DATE_ADD(start, INTERVAL 15 MINUTE) >= NOW();
While this is true for the condition if start+15 mins is greater than NOW()
it is also true for the condition if start is older than NOW(). This the part of the code I am having an issue with.
Then I know I can add some operator AND, >= or, <= with respect to an upcoming timestamp called start or NOW() and also use DATE_ADD or DATE_SUB but I can not seem to find the correct combination of the above if NOW() is within an interval leading up into upcoming timestamp.
If you want to know if we are within 15 minutes of the start time (but only if we're before the time) this will work:
NOW() BETWEEN start - INTERVAL 15 minute AND start
I want to display the data from now until one week ago.
i have query uses the where statement as
WHERE date
BETWEEN
(CURRENT_DATE() - INTERVAL 1 WEEK)
AND
CURRENT_DATE();
It shows data for past 1 week but does not include today. What am i doing wrong here?
current_date will wait till midnight i.e. till the day/date is over. Use now() to display data till the current timestamp.
Make sure the datatypes (date/datetime/timestamp) are consistent. Cast them where ever required.
Important: Avoid using operators with the datatype they are not meant for. I see, you're using - (minus) with date/timestamp. A better way would be : subdate(now(), interval 1 week)
Table: donate
Columns: donate_id, donate_active, donate_time
donate_time value is a unix_timestamp
I have to select all entries 2 weeks before reaching the donate_time, to send a reminder email.
I tried many ways, but I always get a syntax error.
SELECT * FROM donate
WHERE donate_time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 WEEK))
According to this page, INTERVAL only supports WEEK from version 5, and you said you are running 4.1. Try 14 DAY instead of 2 WEEK.
I pasted your code and it works for me in MySQL Workbench 5.2.47.
Make sure you have a space after the table name and a semicolon at the end.
Check any surrounding code for sytax errors.
Here is the code I ran successfully. Try pasting back to your environment (change names).
SELECT * FROM requests
WHERE returntime >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 WEEK))
This query is selecting rows applied last 30 days:
SELECT `amount` FROM `mg_inputs` WHERE `amount`<0 AND `product`='144' AND DATE(firstedit) BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
How about queries selecting rows applied last 30 days, but 1 month ago (between: today-30days and today-60days)? Same question goes for "2 months ago". Nothing is working for me at all (SQL is returning errors).
One important thing to note here is that not all months are 30 days, so instead of using INTERVAL DAY use INTERVAL MONTH.
Next, you don't need to use the subtraction sign for dates, you can use the DATE_SUB() function which will do what you need.
Last, keeping those things in mind, you can use the BETWEEN operator to check for rows within a date range. So, for example, if you want all rows from one month ago, try this:
SELECT *
FROM myTable
WHERE dateColumn BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 MONTH) AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
You should note that for the BETWEEN operator to work properly, the older date must appear first. Here is an SQL Fiddle example that demonstrates that.
I'm trying to get all the records just for today but having trouble. Another thing I'm unsure of how to factor in is that my server time is two hours ahead of my local time so I'll need to figure out the offset. I'm off setting the time when I'm inserting the data just not sure how to do it on retrieval of if I'll need to.
mysql_query("SELECT `* FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= `date` AND `alert_status` ='0'") or die(mysql_error());
I think part of the problem is the <= I tried just using = and == but neither worked.
What exactly is the problem you're running into? What do you mean when you say that it doesn't work?
I see a syntax error (the backtick right before the asterisk), but I'm not sure what your issue is. That DATE_SUB call looks reasonable to me.
Edit: try something like this:
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND CURDATE()
...which is just a fancy way of doing this:
WHERE `date` >= DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND `date` <= CURDATE()