Fetching rows added last hour - mysql

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)

Related

MySQL query last 30 days sort by day

I have a query that I need to modify to use data variables to search counts for the last 30 days from today and group by date. My SQL server's /tmp/ directory (15GB) keeps filling up and the query fails.
SELECT DATE(`date_time`) AS DAY
, COUNT(DISTINCT(rcid)) AS COUNT
, COUNT(DISTINCT(tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE"%VOE%"
AND DATE_SUB(NOW(), INTERVAL 30 DAY) and NOW()
GROUP BY DAY;
Can you run the query manually in a shell? Looks like you're almost there. It may be failing on the group by. At this point in the execution it doesn't know what column DAY is.
Try: GROUP BY DATE(date_time)
Instead of: GROUP BY DAY.
Your date check is missing the column name to test, and also missing the BETWEEN keyword between the two dates.
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%"
AND date_time BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY DAY;
Try this:
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE
"%VOE%" AND `date_time` BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
GROUP BY `date_time`;
Thanks all. I got t to work using CURDATE()
AND `date_time` BETWEEN CURDATE() - INTERVAL 30
SELECT
DATE(`date_time`) AS DAY,
COUNT(DISTINCT (rcid)) AS COUNT,
COUNT(DISTINCT (tunnelip)) AS TAILS
FROM primarydata
WHERE SERVER LIKE "%VOE%" AND `date_time` BETWEEN CURDATE() - INTERVAL 30 DAY AND NOW() GROUP BY DAY;

Return row count between 2 dates always returns 0

wonder if any aficionados can help me. I am trying to get the number of records between two dates, I made this query really simple using some posts i found on here, it always returns 0.
date_added is a timestamp
SELECT COUNT(id)
FROM item
WHERE date_added >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND date_added <= NOW()
I also tried this:
SELECT COUNT(id)
FROM item
WHERE date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
What am I doing wrong?
Use DATE(NOW()) and DATE(date_added) for comparison of dates in mysql. It will give you date part of the timestamp.
For Example
CURDATE() is 2008-11-11 and
NOW() is 2008-11-11 12:45:34
One is date, other is timestamp. You should take either both dates or both timestamp. One solution I've told above.
I re ran this query this morning and it worked just fine, so I'm not sure what went wrong last night other than the hour. Thanks to everyone's input.
SELECT COUNT(id)
FROM item
WHERE date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
worked fine as did
SELECT COUNT(id)
FROM item
WHERE DATE(date_added) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
Hope that may be of use to someone.

How to filter data in mySQL query based on timestamp?

I want to fetch data of last 1 hour from mySQL database. I can certainly do it in java after results have been fetched but i don't want to unnecessarily put load on application. Please help.
select * from tb_data
where createdtime > ADDDATE(NOW(), INTERVAL -1 HOUR)
If the column is of type DATE you can use the DATE_SUB function, which substracts a given time interval from a DATE.
SELECT * FROM [table] WHERE [date_column] > DATE_SUB(NOW(), INTERVAL 1 HOUR)
Documentation
select * from yourtable where created_at between NOW() and date_sub(NOW() , interval 1 HOUR)

Date ranges in SQL

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.

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)