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")
Related
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)
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))
I have a date column in my database. I use SELECT COUNT to calculate the rows between today and 15 days ago:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN CURDATE() - INTERVAL 15 DAY
AND CURDATE()
This SQL statement is working. But now I want use SELECT COUNT to calculate the rows between today(-15 days) and 30 days ago. But I am getting an error when I try the following statement:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN date(CURDATE(),INTERVAL -15 day)
AND date(CURDATE(),INTERVAL -30 day)
Also I want to know how I can SELECT the rows where the date is more than 30 days ago. Can someone help me with this?
You can use the below to get rows between 15 to 30 days old.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY
Similarly you can use below to get rows that are older than 30 days.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date < CURDATE() - INTERVAL 30 DAY
Try This
SELECT * FROM "table name" WHERE "user_id=2"
BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY
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
I'm trying to make a query that will select all my users who's donor status is ending within 10 days.
As i only want to send the message once I want to select all the users who has their donor status ending between 10 and 9 days ahead.
The Donor end date is in this format: 0000-00-00 00:00:00
This is the query I'm currently working around with:
SELECT UserID FROM users_info WHERE donorEnd BETWEEN (NOW() + INTERVAL 10 DAY) AND (NOW() + INTERVAL 9 DAY)
I think you problem is that you are adding a time not a date: NOW() + INTERVAL 9 DAY = 2015-02-27 19:19 not 2015-02-27 00:00
Try use ADDDATE with CURDATE():
SELECT UserID FROM users_info WHERE donorEnd BETWEEN DATE_ADD(CURDATE(), INTERVAL 9 DAY) AND DATE_ADD(CURDATE(), INTERVAL 9 DAY)