So I have 'table1' I need to insert data from 'table2' where 'table2'.'created_at' is from the past 24hrs.
Don't have much experience in SQL so any advice its welcome.
I have this query for now, but the clients using the website are in different timezone.
INSERT INTO 'table1' (
col1, col2, col3, col4
)
SELECT
NOW(), 0, table2.col3, COUNT(*)
FROM
'table2'
WHERE
DATEDIFF(
table2.created_at, CURDATE()
) >= -1
GROUP BY
table2.col3
Can I make the query to get the created_at as a datetime, get the current datetime, and count the data created in the past 24 hours rather than basing the query off of dates?
UPDATE
what I was needed in the where clause is:
WHERE created_at >= NOW() - INTERVAL 24 HOUR
Related
I am trying to solve this. table has cycle and date column, i wanna add one more column as end date with interval of 100 days from current date and wanna display cycle, start_date, end_date as an output.
i have tried this
select cycle, min(start) as startt, max(start) as endd
from cy
group by cycle ;
however no luck till now.
its MYSQL DB latest version.
CREATE TABLE table1 (
`cycle` INTEGER,
`date` DATE,
);
INSERT INTO table1
(`cycle`, `date`)
VALUES
('1', '15-Jan-21'),
('2', '01-Aug-21'),
('3', '08-sep-21'),
('4', '15-Dec-21');
Output :
If I understand correctly, you might want to use LEAD window function with DATE_SUB if your MySQL version supports that.
SELECT *
FROM (
SELECT *,DATE_SUB(LEAD(`date`) OVER(ORDER BY cycle), INTERVAL 1 DAY) endDate
FROM table1
) t1
WHERE endDate IS NOT NULL
sqlfiddle
How can I subtract the value of the last date today from the value of the last date of the previous day in mysql?
suppose today is 2022/05/27
then
80 -20 = 60
datetime,value
2022/05/27 15:25:30 ,20
2022/05/27 11:20:40 ,34
2022/05/26 9:42:10,80
2022/05/26 5:10:36,150
You are probably using MySQL versions below 8.0, which lack the latest features unfortunately. So I wrote the code in 5.7 and tested it in workbench. To make things more interesting, I took the liberty of adding two rows on top of your original table.
insert test values('2022/05/25 15:10:36',200);
insert test values('2022/05/25 11:10:36',280);
select concat_ws(' minus ',t1.dt,t2.dt) as date_gap,
t1.`value`-t2.`value` as value_gap from
(select date(`datetime`) dt,`value` from test where `datetime` in
(select max(`datetime`)mx from test group by date(`datetime`) )
) t1
join
(select date(`datetime`) dt,`value` from test where `datetime` in
(select max(`datetime`)mx from test group by date(`datetime`) )
) t2
on t1.dt = t2.dt - interval 1 day
;
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;
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'
;
I am trying to set up an event/scheduler to grab the last minutes worth of data every single minute of the day with phpmyadmin.
Some how it is missing data out, it picks up some of the data but misses some out.
Can anyone advise on how to tweak my queries or another way how to do this ?
Here are the queries/events I'm using . --
GET ALL DATA FROM TRANDATA WITHIN THIS DATE RANGE -
CREATE TABLE TABLE1 AS SELECT ID_NO, POS, DATE,
TIMESTAMP FROM TRANDATA WHERE DATE
BETWEEN '2014-11-01 00:00:00' AND NOW()
AND POS IN ('IW1','IW2','IW3','IW4','IW5');
SET EVENT TO KEEP BRINGING DATA OVER FROM TRANDATA EVERY MINUTE OF DAY
CREATE event TABLE1GENERATOR ON schedule every 1 minute do INSERT INTO
TABLE1(ID_NO,POS, DATE, TIMESTAMP) SELECT ID_NO,
POS, DATE FROM TRANDATA
WHERE DATE BETWEEN DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND NOW() AND POS IN
('IW1','IW2','IW3','IW4','IW5');
CREATE SECOND TABLE AND FILTER ID_NO FOR FIRST DATE IT SEES (MIN)
CREATE TABLE TABLE1FILTERED AS SELECT TABLE1.*
FROM TABLE1
WHERE TABLE1.DATE BETWEEN '2014-11-01 00:00:00' AND NOW() and NOT EXISTS (
SELECT *
FROM TABLE1 T2_MIN
WHERE T2_MIN.ID_NO=TABLE1.ID_NO
AND T2_MIN.TIMESTAMP< TABLE1.TIMESTAMP
);
CREATE EVENT TO KEEP FILTERING DATA EVERY MINUTE -
CREATE event TABLE1FILTER ON schedule every 1 minute do INSERT INTO
TABLE1FILTERED (ID_NO, POS, DATE, TIME_STAMP) SELECT TABLE1.*
FROM TABLE1
WHERE TABLE1.DATE BETWEEN DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND NOW()
and NOT EXISTS (
SELECT *
FROM TABLE1 T2_MIN
WHERE T2_MIN.ID_NO=TABLE1.ID_NO
AND T2_MIN.TIMESTAMP< TABLE1.TIMESTAMP
);