I have a field named timestamp. This is the last time a member was logged in.
I am looking to include a where clause in a query for something like
WHERE timestamp > todays date - 6 weeks
How would I do this?
I am trying to only include users that have logged in in the last 6 weeks.
Thanks
I find this syntax more readable than date_sub, but either way works.
WHERE timestamp >= NOW() - INTERVAL 6 WEEK
If you want to go by "Today" (midnight) instead "now" (current time), you would use this
WHERE timestamp >= DATE(NOW()) - INTERVAL 6 WEEK
where column>=date_sub(now(), interval 6 week)
This link demonstrates how you might acquire a timestamp of yesterday using the format DATE_ADD(CURDATE(), INTERVAL -1 DAY), therefore your query would probably be:
WHERE timestamp > DATE_ADD(CURDATE(), INTERVAL -42 DAY)
You can use between and now():
select somevalue
from yourtable
where yourtimestamp between now() - interval 1 day and now()
for TIMESTAMP there is a TIMESTAMPADD() function
SELECT TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
this will return the timestemp of 6 weeks ago
or in the case like the question
SELECT * FROM users
WHERE lastlogin > TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
Any luck yet. Have you tried:
>= DATE_SUB(NOW(), INTERVAL 6 WEEK)
Related
I have this record in expiry_date column:
2015-04-30 04:15:29
2015-04-22 06:02:07
I need to select where the record is 26 days from expiring. Right now I'm using this which is not working. No records were selected.
SELECT * FROM `client` WHERE `expiry_date` = DATE_ADD(NOW(), INTERVAL 26 DAY)
I've searched this website and many of the answers are using <= operator. This solution partially work. It selects both of my record when I only need 2015-04-30 04:15:29 in expiry_date column.
How do I exactly select date that is going to expired and not all date?
The easy solution to this is to use the date function:
WHERE DATE(expiry_date) = DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY)
However, this prevents the use of an index on expiry_date. An alternative that does work with indexes is:
WHERE expiry_date >= DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY) AND
expiry_date < DATE_ADD(CURRENT_DATE, INTERVAL 26 + 1 DAY)
The reason you're having this issue is that expiry_date is a type of datetime so the time makes it not equal. Just change your code to be:
SELECT * FROM client WHERE DATE(expiry_date) = DATE(DATE_ADD(NOW(), INTERVAL 26 DAY))
I am trying to pickup 1 day previous records based on few dates and time stamps but my is not retrieving recorders from 00:00:00 to 23:59:00. I am using the follwoing criteria.
and ((cast(from_unixtime(schedule.last_change_timestamp) as date) = (curdate() - interval 1 day))
or (cast(schedule.date_added as date) = (curdate() - interval 1 day))
or (left(jobsheet.actual_completion_date_set, 10) = (curdate() - interval 1 day))
or (cast(from_unixtime(orders.dateadded) as date) = (curdate() - interval 1 day)))),
The records which I am missing are most likely related to jobsheet.actual_completion_date_set which is in this format
'yyyy-mm-dd hh:mm:ss' and I am using left 10 characters for interval. Please could someone provide some help as to how I can force all these dates to select all records from 00:00:00 to 23:59:00 the previous day.
Thanks,
Is this what you want?
left(jobsheet.actual_completion_date_set, 10) = date_format(date_sub(curdate(), interval 1 day, '%Y-%m-%d')
In MySQL, I find it safer to use date_sub() and date_add() for the built-in constants. These are not returned as datetime or date, so strange things can happen with conversions to the dates.
I want to run a MYSQL query to get data for the previous week. The datatype for the date column is DATETIME. Could anyone suggest?
SELECT *
FROM calendar
WHERE dt BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE();
Here is an another version:
SELECT * FROM table WHERE
YEARWEEK(`date`, 1) = YEARWEEK( CURDATE() - INTERVAL 1 WEEK, 1)
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Here is the solution I find most reliable for getting data between the previus monday to the current monday. (That is what most people mean when the say past week, but not all, and that reflect in mysql).
SELECT
*
FROM
table
WHERE
date BETWEEN
(CURDATE() - INTERVAL 1 DAY) + INTERVAL -1 WEEK - INTERVAL WEEKDAY((CURDATE() - INTERVAL 1 DAY)) DAY
and
(CURDATE() - INTERVAL 1 DAY) + INTERVAL 0 WEEK - INTERVAL WEEKDAY((CURDATE() - INTERVAL 1 DAY)) DAY
It's also easy to change it for another week intervall
Make variable for current datetime - 1 week and make this query:
SELECT * FROM table WHERE date > $datatime
I need to find the account created for the current day, et for the last 7 days.
To find my results for today, it works, and I do this :
SELECT * FROM `account` where DATE(created_at) = DATE(NOW())
But I don't know how to do to get the last 7days account.
I tried something like this, but without success :
SELECT * FROM `account` where DATE(created_at) BETWEEN DATE(NOW()) AND DATE(NOW()-7)
Have you an idea ?
in mysql:
SELECT * FROM `account`
WHERE DATE(created_at) > (NOW() - INTERVAL 7 DAY)
Try:
BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
If created_at has an index and you wouldn't like to prevent the optimiser from using it, I would recommend the following pattern (assuming created_at contains both date and time):
WHERE created_at >= CURRENT_DATE - INTERVAL 7 DAY
AND created_at < CURRENT_DATE + INTERVAL 1 DAY
This spans the range from the day exactly one week ago till today (inclusive), so 8 days in total.
also have a look at MySQL functions ADDDATE(), DATE_ADD(), DATE_SUB()
e.g.
ADDDATE(DATE(NOW()), -7)
In MYSQL DB I need to check if a "datetime" field is more than 24hours (or whatever) ago in which case delete the row.
How to add hours to datetime in mysql?
thanks
Luca
What about something like this :
delete
from your_table
where your_field <= date_sub(now(), INTERVAL 1 day)
With :
now() : the current date time
date_sub() to substract 1 day to that date
Or, if you want o use 24 hours instead of 1 day :
delete
from your_table
where your_field <= date_sub(now(), INTERVAL 24 hour)
You have the Date and Time functions.
WHERE `yourDate` < DATE_SUB(NOW(),INTERVAL 1 DAY)
or shorter
WHERE `yourDate` < NOW() - INTERVAL 1 DAY
there is the addtime() method in mysql
DELETE
FROM table
WHERE datetime_field < DATE_SUB(NOW(), INTERVAL 1 DAY);