datediff() not ignoring time - mysql

I'm using MYSQL 5.5.47 on Debian and I have this bit in my query:
AND DATEDIFF(CURRENT_DATE(), shouts_stores.date) <= 7
-- changed it from AND DATEDIFF(NOW(), shouts_stores.date) <= 7
The problem?
Datediff, according to everything I've read, is supposed to ignore the time part.
I have to get rows of the last 7 days, but I need the timestamps with a time element for "Posted X seconds/minutes/hours/days ago" functionality in PHP.
However, when it comes to just getting the rows, I simply need the posts from up to (and including) 7 days (or 1 week) ago. However, if the posting time is earlier than it is right now, it'll knock it up to 8 days. This causes some confusion. Does anyone have any idea about this?
edit: example
This is what the table looks like:
tl dr; I need DATEDIFF() to ignore the time part of the datetime.

Try:
... DATEDIFF(CURRENT_DATE(), DATE(shouts_stores.date)) <= 7

Try this:
DATE_ADD(shouts_stores.date,INTERVAL 7 DAY) >= CURRENT_DATE()
replace CURRENT_DATE() with NOW() as needed

Related

MySQL DATE_ADD statement according to the book but wont return result

I have a statement that I believe to be correct and yet it returns a '0' results when the number is actually '4'. It comes back as a valid statement through PHPMyAdmin. Can someone tell me what I am missing about this? When using the DATE_SUB it works properly in look backward 7 days. I am trying the DATE_ADD to look forward:
SELECT DAYNAME(table_name..`column_one`) ,
`column_two`,
`column_three`,
`column_four`
FROM table_name
WHERE `column_one` BETWEEN DATE_ADD(CURDATE() ,INTERVAL 7 DAY) AND CURDATE()
BETWEEN expects a lower and an upper bound, in that order; you have them reversed. Try:
BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 7 DAY)
Documentation is here:
mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;
returns:
-> 1, 0
Note that with DATE_SUB (assuming a positive number of days), you will want that first, since it will be the lesser value.

how to display data from now until one week ago in mysql

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)

Mysql between reverse range

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 :)

How do I select two weeks ago in MYSQL?

I have a report that is driven by a sql query that looks like this:
SELECT batch_log.userid,
batches.operation_id,
SUM(TIME_TO_SEC(ramses.batch_log.time_elapsed)),
SUM(ramses.tasks.estimated_nonrecurring + ramses.tasks.estimated_recurring),
DATE(start_time)
FROM batch_log
JOIN batches ON batch_log.batch_id=batches.id
JOIN ramses.tasks ON ramses.batch_log.batch_id=ramses.tasks.batch_id
JOIN protocase.tblusers on ramses.batch_log.userid = protocase.tblusers.userid
WHERE DATE(ramses.batch_log.start_time) > "2011-02-01"
AND ramses.batch_log.time_elapsed > "00:03:00"
AND DATE(ramses.batch_log.start_time) < now()
AND protocase.tblusers.active = 1
AND protocase.tblusers.userid NOT in ("ksnow","smanning", "dstapleton")
GROUP BY userid, batches.operation_id, date(start_time)
ORDER BY start_time, userid ASC
Since this is to be compared with the time from the current payperiod it causes an error.
Our pay periods start on a Sunday, the first pay period was 2011-02-01 and our last pay period started the 4th of this month. How do I put that into my where statement to strip the most recent pay period out of the query?
EDIT: So now I'm using date_sub(now(), INTERVAL 2 WEEK) but I really need a particular day of the week(SUNDAY) since it is wednesday it's chopping it off at wednesday.
You want to use DATE_SUB, and as an example.
Specifically:
select DATE_SUB(curdate(), INTERVAL 2 WEEK)
gets you two weeks ago. Insert the DATE_SUB ... part into your sql and you're good to go.
Edit per your comment:
Check out DAYOFWEEK:
and you can do something along the lines of:
DATE_SUB(DATE_SUB(curdate(), INTERVAL 2 WEEK), INTERVAL 2 + DAYOFWEEK(curdate()) DAY)
(I don't have a MySql instance to test it on .. but essentially subtract the number of days after Monday.)
Question isn't quite clear, especially after the edit - it isn't clear now is the "pay period" two weeks long or do you want just last two weeks back from last sunday? I assume that the period is two weeks... then you first need to know how many days the latest period (which you want to ignore, as it isn't over yet) has been going on. To get that number of days you can use expression like
DATEDIFF(today, FirstPeriod) % 14
where FirstPeriod is 2011-02-01. And now you strip that number of days from the current date in the query using date_sub(). The exact expression depends on how the period is defined but you should get the idea...

MYSQL - how to get a time difference?

In MySQL I am trying to select rows from a table that have a lock_dt older than 10 hours. How can I write this sql statement properly?
select phone from table where ((now() - lock_dt) < 10 hours)
SELECT phone
FROM table
WHERE lock_dt > NOW() - INTERVAL 10 HOUR
Using intervals is pretty handy. Also, I try to isolate the column so an eventual index can be used (sometimes, the RDBMS don't know how to use an index with NOW() - lock_dt, even if there's an index on lock_dt).
Also, the description of your problem contradicts your query. NOW() - lock_dt < 10 hours means the interval is less than 10 hours. That's what my query do. You have to change > to < if you want more than 10 hours.
SELECT phone FROM table WHERE lock_dt < DATE_SUB(now(), interval 10 hour);