mysql NOW() with defined time - mysql

I need to perform a mysql select query where date time field in table is less then todays date with a time of 2am.
How would I structure this
SELECT * FROM tbl WHERE datetimefield < (DATE(NOW()) and time 2am)
Thank you for any assistance.

This worked:
SELECT * FROM tbl WHERE datetimefield < DATE_ADD(CURDATE(), INTERVAL 2 HOUR)
fiddle - in the fiddle I used a hard coded date '2013-05-04', so it would work correctly later.

SELECT * FROM tbl WHERE datetimefield < STR_TO_DATE(concat(curdate(), ' ', '02'),'%Y-%m-%d %H')

Related

MySql, How to display data that was entered in the last 3 hours?

I would like to output recent data that was entered to the DB in the last Three hours, for that I have done this :
SELECT * FROM `tableName` WHERE DATE <= TIMEDIFF ( 'SYSDATE()', '03:00:00' )
But it does NOT worked for me, any ideas on how I can do it ?
You can do this :)
SELECT *
FROM tableName
where Date >= DATE_SUB(NOW(),INTERVAL 3 HOUR);
use timestampdiff on your date column.
SELECT * FROM tableName
WHERE timestampdiff( HOUR, date, now() ) <= 3
Provided your column date must be of type datetype or timestamp

how to filter a select statement in mysql for a particular time period without using now()

I have tried to filter records but with the use of now function as given below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
What I need is a select statement that can filter its records for a week or month from the current date but without using NOW() function
if you are using java you could make use of the following code
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
or you could use curdate() of mysql
Since I found it hard to understand the question I provide the following possibilities:
Try for all dates in a week from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 WEEK)
and for all dates in a month from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 MONTH)
If you are looking for all dates of the current month use
SELECT * FROM table
WHERE MONTH(date)=MONTH(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
or for all dates of the current week use
SELECT * FROM table
WHERE WEEK(date)=WEEK(CURDATE()) AND YEAR(date)=YEAR(CURDATE())

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)

Return rows between two dates in MySQL

I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011
From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.

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)