MySQL TIMESTAMP BETWEEN 2 hours ago and 3 days ago - mysql

Trying to do MySQL query on a TIMESTAMP Field.
I get close to getting the correct result but it always does just 3 days behind, nothing in the current day. I believe it has something to do with utilizing BETWEEN with a TIMESTAMP field.
SELECT
billing_first_name,
cart_id,
placed_ts,
s_email
FROM `orders`
WHERE `paypal_response` IS NULL
AND `authorize_response` IS NULL
AND `s_email` IS NOT NULL
AND (`placed_ts` BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL 3 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 2 HOUR))
GROUP BY `cart_id`
ORDER BY placed_ts DESC

It's because you're using DATE(NOW()) instead of just NOW(). This discards the time of day, so it subtracts 2 hours from the beginning of the day (i.e. it returs 10pm on the previous day), rather than 2 hours before now.
AND (`placed_ts` BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 2 HOUR))

Related

MySQL select complete last month

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)

mysql query how to show each day's total cash sale for the current week

I have the following mysql query which shows the each day's total cash sale for the current week.
SELECT
sum(Price) as totalprice,
WEEKDAY(CreatedOn) as dayno,
DATE(CreatedOn) as CreatedOn,
AgentID
FROM records
WHERE CreatedOn BETWEEN (CURDATE()-WEEKDAY(CURDATE())) AND CURDATE()
GROUP BY DATE(CreatedOn)
When I run the query it looks like this:
There are records on 30th November(today's date). So,
day 0 (Monday) no cash sale
day 1 (Tuesday) $5049
day 2 (Wednsday) $99
Nothing is displayed for day 3 (Thursday/today). I cannot figure out the reason there are definitely record in the database but cannot get them to be displayed. I would appreciate any help.
CURDATE() is today's date but at 00:00:00+0000000
"push up" the higher date by 1 day and avoid using between for date/time ranges:
WHERE CreatedOn >= date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
AND CreatedOn < date_add(CURDATE(), INTERVAL 1 DAY)
select date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
, date_add(CURDATE(), INTERVAL 1 DAY)
The condition in the query currently specifies on or before midnight today, so any rows for today after midnight are going to be excluded.
I think you are intending to specify CreatedOn before midnight of the following day.
I also suggest you don't subtract an integer value from a date/datetime, and instead use the INTERVAL syntax
WHERE CreatedOn >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
AND CreatedOn < CURDATE() + INTERVAL 1 DAY
To test those expressions before we include them in a query, we can run a SELECT statement:
SELECT CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
, CURDATE() + INTERVAL 1 DAY
and verify that those expressions are returning what we expect, the values we want to use. For testing, we can replace CURDATE() with a date value to test the return for days other than today.

get sql results that have a timestamp date that is n number of days from today

most of the results ive found while looking this up have been from getting a range of dates from today to say 30 days from now. Im just looking for the results that 14, 7, 3 and 1 day from today (each individualy)
Heres the sql i have so far
SELECT * FROM Location WHERE timestamp (CURDATE() + 14 Day)
that would be the sql to get all locations that have a timestamp that is 14 days from now (this is to send out a reminder newsletter fyi).
And it shouldnt matter what time of that day it is, if possible it should grab all timestamp results from that day
SELECT * FROM Location
WHERE DATE(timestamp) IN (
DATE_ADD(CURDATE(), INTERVAL 14 DAY),
DATE_ADD(CURDATE(), INTERVAL 7 DAY),
DATE_ADD(CURDATE(), INTERVAL 3 DAY),
DATE_ADD(CURDATE(), INTERVAL 1 DAY)
);
Reference: MySQL add days to a date

How do I get Timestamp minus 6 weeks in MySQL?

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)

mysql date & timestamp comparison help

hoping you guys can help - I have a timestamp column in my db table and i want to run a query to find any records from that table where the timestamp is the same as 7 days from now (ignoring the hours, minutes and seconds). So far I have this but I'm certain it's not what i should be doing:
WHERE `import_date` = 'DATE_SUB(NOW(), INTERVAL 7 days)'
WHERE import_date = 'DATE_SUB(NOW(), INTERVAL 7 days)'
should be
WHERE import_date >= DATE_SUB(NOW(), INTERVAL 7 day)
days should be day
for timestamp same as 7 day before from now use
WHERE DATE_FORMAT(`import_date`, "%Y-%m-%d") = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 day), "%Y-%m-%d")