All MySQL records from yesterday - mysql

What is an efficient way to get all records with a datetime column whose value falls somewhere between yesterday at 00:00:00 and yesterday at 23:59:59?
SQL:
CREATE TABLE `mytable` (
`id` BIGINT,
`created_at` DATETIME
);
INSERT INTO `mytable` (`id`, `created_at`) VALUES
(1, '2016-01-18 14:28:59'),
(2, '2016-01-19 20:03:00'),
(3, '2016-01-19 11:12:05'),
(4, '2016-01-20 03:04:01');
If I run this query at any time on 2016-01-20, then all I'd want to return is rows 2 and 3.

Since you're only looking for the date portion, you can compare those easily using MySQL's DATE() function.
SELECT * FROM table WHERE DATE(created_at) = DATE(NOW() - INTERVAL 1 DAY);
Note that if you have a very large number of records this can be inefficient; indexing advantages are lost with the derived value of DATE(). In that case, you can use this query:
SELECT * FROM table
WHERE created_at BETWEEN CURDATE() - INTERVAL 1 DAY
AND CURDATE() - INTERVAL 1 SECOND;
This works because date values such as the one returned by CURDATE() are assumed to have a timestamp of 00:00:00. The index can still be used because the date column's value is not being transformed at all.

You can still use the index if you say
SELECT * FROM TABLE
WHERE CREATED_AT >= CURDATE() - INTERVAL 1 DAY
AND CREATED_AT < CURDATE();

You can use subdate to indicate "yesterday" and use date() to indicate that you want records where just the date part of the column matches. So:
SELECT *
FROM tablename
WHERE DATE(created_at) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)

Here is the same question with an answer. To summarize answer for you, use subdate() as suggested by Sajmon.
subdate(currentDate, 1)
using your table it should be.
select *
from tablename
where created_at between subdate(CURDATE(), 1)
and date (now() )

use:
subdate(current_date, 1)
it's awesome for your case!

SELECT subdate(current_date(), 1)
SELECT * FROM table
WHERE created_at >= subdate(current_date(), 1)

You can use this, just put tablename and columnName (Which Contain 2021/01/09 or 2022-01-11 14:56:07 etc)
select * from (TABLENAME) where DATE(columnNAME) = TODAY - 1;

Related

Get records in Mysql where unix timestamp is today

I'm storing records in msyql where a resolve_by column has a unix timestamp.
I'm trying this query:
SELECT id FROM tickets WHERE FROM_UNIXTIME('resolve_by','%Y-%m-%d') = CURDATE()
The basic table structure is:
id|resolve_by|date_created
4, 1506092040, 1506084841
But this is returning 0 records. How can I get records where the unix timestamp value = today's date?
Thanks,
Changed query from :
SELECT id FROM tickets WHERE FROM_UNIXTIME('resolve_by','%Y-%m-%d') = CURDATE()
To:
SELECT id FROM tickets WHERE FROM_UNIXTIME(resolve_by,'%Y-%m-%d') = CURDATE()
It's working now.
In general you'll want to avoid using functions on the columns side of where conditions, as it will most probably disqualify your query to benefit from indexes.
Consider something like:
create table test_table ( id varchar(36) primary key, ts timestamp );
insert into test_table (id,ts) values('yesterday', current_timestamp - interval 1 day);
insert into test_table (id,ts) values('midnight', current_date);
insert into test_table (id,ts) values('now', current_timestamp);
insert into test_table (id,ts) values('next midnight', current_date + interval 1 day);
insert into test_table (id,ts) values('tomorrow', current_timestamp + interval 1 day);
create index test_table_i1 on test_table (ts);
select *
from test_table
where ts >= current_date
and ts < current_date + interval 1 day;
;
PS: you can also use
select *
from test_table
where ts between current_date and current_date + interval 1 day;
if you're not picky about excluding next midnight (between accepts both boundaries)

mysql query to find record filtering on the datetime

I have a table in which the following data structure is used:
col1 col2 timestamp
value1 value11 2014-27-04 03:05:25
value2 value22 2014-28-04 03:05:25
value3 value33 2014-27-04 04:05:25
Now I want to retrieve the rows which timestamp is greater than equal to 03:05:25 hours but that should be today's data...not the previous days data.
To avail this I have used the following query but this
select * from tab1 where time(timestamp) > '03:05:25';
But this returns all the data of previous days also. Any help on this is will be very helpful.
You could use
SELECT * FROM table WHERE TIME(timestamp)>01:01:01 AND DATE(timestamp) = DATE(NOW())
Wouldnt that fit your need?
Mel_T's will work too. Either use concat to bring it together or split it into 2 filters (time and date).
To select all rows of the same day but later than 03:05:25 you can do this:
select * from tab1 where timestamp > CONCAT(CURDATE(),' 03:05:25');
select
`timestamp` AS `timestamp`,
date(`timestamp`) AS `date`,
time(`timestamp`) AS `time`
from `timestamps`
where date(`timestamp`) = date(now()) AND time(`timestamp`) >= '03:05:25'
;

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 can I filter a query by the hour portion of a DateTime field in MySQL?

I need to select rows from table, where e.g. time is >= 18:00:00 no matter of date. Problem is that value is datetime type so there is also date beside. e.g. 2012-01-25 18:00:00.
table1
======
row 1: id='1' datetime='2012-01-25 18:00:00'
row 2: id='2' datetime='2012-01-27 15:00:00'
row 3: id='3' datetime='2012-01-30 19:45:00'
I need to select row 1 and row 3.
Is there way to combine LIKE and >= to do time >= '% 18:00:00' where % represents whatever date?
You can use the TIME() function:
WHERE TIME(`datetime`) >= '18:00:00'
select *
from table1
where HOUR(datimetime) >= 18
Something like this maybe:
SELECT *
FROM yourTable
WHERE
EXTRACT(HOUR FROM YourDate)>18

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.