I have a database column called time
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
But when I run the following query using PDO:
DELETE FROM `table` WHERE time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :days DAY))
It throws the following exception:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1555980012' for column 'time' at row 1
I don't understand why this is happening, could any one please explain?
You do not need to use UNIX_TIMESTAMP function in your where clause to convert date to number.
This should work with no issue:
DELETE FROM `table` WHERE `time` < DATE_SUB(NOW(), INTERVAL :days DAY)
Related
This statement works fine:
SELECT * FROM table_name
WHERE DATE(date_event) < DATE(NOW() - INTERVAL 90 DAY);
Using the same WHERE clause in DELETE statement does not works :
DELETE FROM table_name
WHERE DATE(date_event) < DATE(NOW() - INTERVAL 90 DAY);
Return message :
Error Code: 1292. Truncated incorrect datetime value: '2018-01-10T13:22:29.000000Z'
Datatype of field "date_event" is CHAR(27).
Testing DATE() function with next SQL statement works fine :
SELECT DATE('2018-01-10T13:22:28.000000Z');
'2018-01-10'
Here the complete SQL code :
CREATE TABLE `table_name` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_event` char(27) NOT NULL COMMENT 'YYYY-MM-DDThh:mm:ss.ffffffZ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='Test';
INSERT INTO table_name
(date_event) VALUES ('2018-01-10T13:22:28.000000Z');
SELECT * FROM table_name
WHERE DATE(date_event) < DATE(NOW() - INTERVAL 90 DAY);
DELETE FROM table_name
WHERE DATE(date_event) < DATE(NOW() - INTERVAL 90 DAY);
date_event field is Char (string type). We will need to convert it to MySQL datetime format using Str_to_Date() function:
'2018-01-10T13:22:28.000000Z' can basically be written in terms of format specifiers as: '%Y-%m-%dT%T.%fZ'.
So, your Delete query should be as follows (DB Fiddle DEMO):
DELETE FROM table_name
WHERE STR_TO_DATE(date_event, '%Y-%m-%dT%T.%fZ') < DATE(NOW() - INTERVAL 90 DAY);
And, your Select query should be:
SELECT * FROM table_name
WHERE STR_TO_DATE(date_event, '%Y-%m-%dT%T.%fZ') < DATE(NOW() - INTERVAL 90 DAY);
Additional Details:
%Y Year as a numeric, 4-digit value
%m Month name as a numeric value (00 to 12)
%d Day of the month as a numeric value (01 to 31)
%T Time in 24 hour format (hh:mm:ss)
%f Microseconds (000000 to 999999)
Complete list of format specifiers can be seen at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I'm trying to sum all my elements of a specific type in a json column of the last 24 hours and I can't figure out my query.
So I have a json column called data and i want the "live" key to have the sum of all "live" in the last 24 hours and for that I was trying to do something like this
select sum(data->>'live'::json)
from probing
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47'
and timestamp > current_timestamp - interval '1 day'
order by timestamp desc;
I know this doesn't work and I can't figure a viable solution to this. I saw some solutions including using "json_array_elemens" or "json_each" but can't make this work.
I came with this solution:
select
sum(cast(data->>'live' as integer))
from
probing
where
device_id='f051b333-8f1f-4e65-9acc-e76470a87f47'
and timestamp > current_timestamp - interval '1 day';
U can query in both the ways:
select sum((data->>'live')::int)
from probing
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47'
and timestamp > current_timestamp - interval '1 day'
order by timestamp desc;
select sum(cast(data->>'live' as int))
from probing
where device_id='f051b333-8f1f-4e65-9acc-e76470a87f47'
and timestamp > current_timestamp - interval '1 day'
order by timestamp desc ;
I'm facing a strange mysql behavior...
If I want to return the rows from "MyTable" with a date lower than date-10 seconds ago or a future date
I also store future date because in my real program, I "launch" some queries with delay and date is actually the last query date...i.e.: a kind of queue...:
SELECT (NOW() - date) AS new_delay, id
FROM MyTable
WHERE (NOW() - date < 10)
ORDER BY new_delay DESC;
This one does not work as expected: It returns all the entries:
EDIT: here is the result:
However, this one is working just fine:
SELECT (NOW() - date) AS new_delay, id
FROM MyTable
WHERE (NOW() < date + 10)
ORDER BY new_delay DESC;
DB example:
CREATE TABLE IF NOT EXISTS `MyTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
INSERT INTO `MyTable` (`id`, `date`) VALUES
(1, (NOW())),
(2, (NOW()-10)),
(3, (NOW()+100));
Any ideas??
Don't do the comparisons like that. In a numeric context now() end up being converted to an integer -- and in an arcane format. Instead, use DATEDIFF() or just regular comparisons. For instance, if you want the difference in days:
SELECT datediff(curdate(), date) as new_delay, id
FROM MyTable
WHERE date >= date_sub(now(), interval 10 day)
ORDER BY new_delay DESC;
use mysql DATEDIFF
select DATEDIFF(curdate(),date) as new_delay, id from MyTable
where date >= date_sub(curdate(), interval 10 day)
ORDER BY new_delay DESC;
DATEDIFF() function returns the time between two dates
As proposed by #Gordon in the his answer, I can use the date_sub / date_add functions...
I can correct the where clause to be :
WHERE NOW() < date_add(ServerRequests.date, interval 10 second)
OR
WHERE date > date_sub(now(), interval 10 second)
OR as proposed in my initial post:
WHERE (NOW() < date + 10)
But I still don't see why I cannot use the sub operation...So if anyone can give me a reason, I would be happy to understand...
I am looking for a solution to find with mySQL every result that was created since a(or many) entire week(s) (7 days).
I tried this but it seems sometimes the result is false because is there more than one result per week.
SELECT *
FROM `datatable`
WHERE MOD(TIMESTAMPDIFF(DAY,UNIX_TIMESTAMP(created),NOW()),7)=0;
created is a timestamp.
Thanks for any response!
Try this:
SELECT
*
FROM
`datatable`
WHERE 1
AND `created` >= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 00:00:00')
AND `created` <= DATE_FORMAT(NOW() - INTERVAL 7 DAY,'%Y-%m-%d 23:59:59')
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument as
seconds since '1970-01-01 00:00:00' UTC, date may be a DATE string, a
DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD. The server interprets date as a value in the current time
zone and converts it to an internal value in UTC. Clients can set their
time zone as described in
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html.
So, do not use UNIX_TIMESTAMP() function, but simple DATE().
As far as I can understand the question, you want the records that are are newer than a certainDate, AND not older than 7 days, or 14 days,..., 140 days (whichever of them is closest to the certainDate) from current date.
IF I understand you correctly, following might work for you with certainDate = '2013-04-01 00:00:00'
SELECT *
FROM `datatable`
WHERE `created` >= ADDDATE(
'2013-04-01 00:00:00',
INTERVAL MOD(ABS(TIMESTAMPDIFF(DAY, NOW(), '2013-04-01 00:00:00')),7) DAY
)
This query will fetch all results having created in last 14 days.
I cant seem to get this to work, It returns Null
SELECT sdt, timeFor, DATE_ADD(TIMESTAMP(sdt), INTERVAL timeFor MINUTE) FROM tbl_day
The return keeps returning
sdt, timeFor, DATE_ADD(TIMESTAMP(sdt), INTERVAL timeFor MINUTE)
'0000-00-00 01:00:00', 15, ''
Columns Type
sdt DATETIME
timeFor BIGINT(20)
Any ideas
MySQL usually returns NULL on date/time operations when column value is incomplete datetime. Something like 2010-00-05 11:22:33, etc. Also using timestamp function on sdt column might not be a good idea. I'd suggest providing normal datetime value.