Date ranges in SQL - mysql

I'm having trouble coming up with this solution logically. I have an accounts table with a datetime trial_expiration_date column. I'd like to return all accounts that have been expired for at least two weeks but no more than one month using this column. How can I achieve this?

Something like this should work. Just select all records where the expiration date is between two weeks ago and one month ago.
select *
from accounts
where trial_expiration_date between DATE_SUB(curdate(), INTERVAL 1 MONTH)
and DATE_SUB(curdate(), INTERVAL 2 WEEK)

How about this..
select *
from TABLE
where trial_expiration_date between dateadd(day,-14,getdate()) and getdate()

You can do
select *
from t
where
datediff(date_add(trial_expiration_date, interval 2 week), now()) < 0
and
datediff(date_add(trial_expiration_date, interval 1 month), now()) > 0
It only calculate the date parts only and does not consider time.

Related

Get number of records in table during last day and last week with single query in MySQL 5.5

I have table with timestamp in one column (MySQL 5.5). Is it possible to get number of records during last 1 day (last 86400 seconds) and number of records during last week (last 604800 seconds) in single query?
I know how to do it with 2 queries, but it would be nice to know if there is some neat solution to this.
timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
timestamp > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Quick and Dirty is just a Union then.
Select '1 Days', Count(*) as NumberOf from sometable
Where `timestamp` > DATE_SUB(NOW(), INTERVAL 1 DAY)
union
Select '7 Days', Count(*) as from sometable
Where `timestamp` > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Gets painful and perhaps expensive if you have a lot of ranges though. f So you might want to look at DateDiff to calculate the interval once and then count that.

MySQL - Select Month difference

So I can get the days with SELECT DATEDIFF(CURDATE(),expires) but not exactly a month with an output of one (1). Isn't this anyhow possible to transform it to month in MySQL? I want to check if a given date (like expires) is >= 1.
SELECT
...
FROM
tablename
WHERE
expires<=DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
Will get you all rows, that have expired a month ago or earlier
Use following query
Select * from
table_name where TIMESTAMPDIFF
(MONTH,
ADDDATE(LAST_DAY(SUBDATE(expires, INTERVAL 1 MONTH)), 1),
ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
)>=1

How to do : "Between TODAY AND TODAY-7"?

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)

Fetch rows with DATE

I am storing rows with MySQL using a date col. Everytime I insert a new row I use date=CURDATE() .
Is there anyway I can run a SELECT statement that selects all rows that were created within the last 24 hours?
Joel
Try this:
SELECT * FROM table WHERE date >= DATE_SUB(NOW(), INTERVAL 24 HOUR))
You can use the sub_date() function with interval to make a range of dates between now and 24 hours ago:
SELECT * FROM
table WHERE
date_field BETWEEN curdate() AND DATE_SUB(curdate(), INTERVAL 1 DAY);

Fetching rows added last hour

I keep a record of logins in a table. I have columns for id, ip, date and time. From that record of logins I wanna fetch logins made only in the last hour.
I'm sweeping through the MySQL docs on time and date functions, but I just can't seem to combine them correctly.
Can somebody help me?
Make use of the DATE_SUB() and NOW() functions:
select count(*) as cnt
from log
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Hope it helps you : )
If you want to implement this into a cronjob, you need to specify the start and end.
For example, at 2pm, if you want to get the data for the past hour from 13:00:00 until 13:59:59, here's how to do it:
dateField BETWEEN
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %H:00:00')
AND
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %H:59:59')
it can be done easily using
select count(*) from logins where datetime>= NOW()- INTERVAL 1 HOUR
I recommend have one datetime column instead of date and time columns.
Suppose you have a datetime column called last_login:
SELECT id, ip_address, last_login
FROM mytable
WHERE last_login >= DATE_SUB(NOW(), interval 1 hour);
without the specifics, I think Date_Add() would work.. by adding to your where clause an add of NOW negative hours
(or Date_Sub() )
You can also use CURDATE()function
SELECT
count(*) AS TotalCount
FROM
tblName
WHERE
datetime >= DATE_SUB(CURDATE(), INTERVAL 1 HOUR)