Calculate date diff in sql - mysql

I have a table which has the following columns -
Id end_time status
12 1407477033 success
13 1407479820 error
14 1407479974 error
I have to find all the id's which have status 'error' and were completed in the last 15 minutes. I am using ruby to interact with the database, so my query becomes ( the table name is q_lists ). To find the time difference I am using DATEDIFF -
date = QList.find_by_sql("select end_time from q_lists where id = #{a.id}")
diff = QList.find_by_sql("select DATEDIFF(minute, FROM_UNIXTIME(date), CURRENT_TIMESTAMP")
Note : a is the loop variable,I am iterating for each row.
I am getting the following error -
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'DATEDIFF'
How can I resolve this. Also can I write the query in a way all the id's are stored in an array using just one sql statement (without loop iterations).
EDIT
TIMESTAMPDIFF solves the problem. Sorry for squishing two questions in one, but can someone point the correct way of getting all id's in just one go without using loops.

MySQL's DATEDIFF is not the same as Microsoft's. It does not include the first parameter, and will only return the number of days between two dates. You want TIMESTAMPDIFF

Related

Retrieve rows from DB if Date-Difference condition apply

I need to retrieve rows from SQL database where a time-difference condition apply.
Basically I need to retrieve rows where date difference from Now() is < x minutes.
Column datetime has this format: 2022-12-05 15:01:43
I tried
SELECT * FROM copytrade WHERE DATEDIFF(minute, datetime, GETDATE() AS DateDiff) < 10
Using this select I get error "Incorrect parameter count in the call to native function 'DATEDIFF'
Is it possible in a single SQL line to achieve this select?
Thanks

Difference between two dates in MySQL with DATE() function

I am using DATE() function to calculate the difference between two dates in MySQL
value of SYSDATE() function is following
select SYSDATE();
2020-07-15 12:16:07.0
When I am using date from same month, it is giving correct result
select DATE(SYSDATE())- DATE('2020-07-13');
2
But when I am using date from last month it is giving difference as 86 instead of 16;
select DATE(SYSDATE())- DATE('2020-06-29');
86
Edit:
I am aware that we can use DATEDIFF() but I want to verify why DATE() function is giving results like this since we are already using this in code
MySQL doesn't support subtracting one date from another. The code
SELECT DATE '2020-07-15' - DATE '2020-06-29';
should hence result in an error, but MySQL silently converts this to this instead:
SELECT 20200715 - 20200629;
Seeing that you want to subtract two values, it assumes that you want to work with numbers. Dates are not numbers, but their internal representation yyyymmdd can be represented numerically. So, while CAST(DATE '2020-07-15 ' AS int) fails with a syntax error, as it should, MySQL is not consistent, when it comes to subtraction. It generates the numbers 20200715 and 20200629 and works with these.
I consider this a bug. MySQL should either raise an exception or return an INTERVAL when subtracting one DATE from another.

MySQL timediff returning unexpected results

Please consider the following query:
SELECT submitted_time FROM jobs WHERE timediff(NOW(), submitted_time) < '24:00:00'
My hope is for this to return all rows that have a "submitted_time" column containing a timestamp that was within the last 24 hours, However I am receiving the following results:
2017-01-18 14:58:34
2017-01-16 14:58:34
If I run the query SELECT NOW() I get 2017-01-25 18:58:32
Which appears to be correct.
What is stranger still is that I have more recent rows in the DB such as:
2017-01-24 15:17:13
Which are not being returned.
I hope I have made a glaringly obvious error that someone can point out, rather than beginning the descent into madness.
Just to be clear, the simplest and probably most performant way to handle this is (as per the link I provided in the comment)
SELECT submitted_time FROM jobs WHERE submitted_time > DATE_ADD(NOW(), INTERVAL -1 DAY);
This should be all jobs submitted literally within the last 24 hours at the moment the query is issued.
This might not be important to you for this query, but whenever you apply functions to columns in your table, any indexes you might have can not be used, because the database must run the function(s) on each value in the table before it can perform a comparison.
Using this method you figure out what the comparable datetime needs to be and mysql will use an index on submitted_time for the comparison, assuming that column is indexed appropriately.

Get all rows with specific time difference

Hello i have a table like this:
|user_name|pw|register_date|last_time_entered
I want to get all the rows where last_seen_date - register_date < 7
I dont know how to write this query i thought about something like this
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(day, workoutlog_1.personal.register_date, workoutlog_1.personal.last_time_entered) < 7;
But i get this error:
Error Code: 1582. Incorrect parameter count in the call to native function 'DATEDIFF'
Thanks for helping.
Your error code seems to come from mysql.
With mysql, datediff takes only 2 parameters (day is not needed)
I think that in (really) old versions, it took 3 parameters, now it works only with 2, and it will return days.
If you had to work with another unit (hour for example), you could use TIMESTAMPDIFF
MySql's DATEDIFF differs from SqlServers DATEDIFF in that it takes only 2 date parameters, and returns the difference in days. Since you want days anyway, just remove the day parameter, i.e.
SELECT * FROM workoutlog_1.personal
WHERE DATEDIFF(workoutlog_1.personal.register_date,
workoutlog_1.personal.last_time_entered) < 7;

Querying results on timestamp in mysql

I have series of records in a table called 'hits' and each record has the current_timestamp (ie. 2010-04-30 10:11:30) in a column called 'current_time'.
What I would like to do is query these records and return only the records from the current month. I cannot seem to get this to work.
I have tried a range of queries that don't work such as -
Select * FROM hits WHERE MONTH(FROM_UNIXTIME(current_time)) = 4
I don't know if I am even on the right lines!
Can anyone point me in the right direction?
Cheers.
WHERE MONTH(current_time) = 4
as your timestamp is not a unix one.
also I suppose you want to check YEAR() as well
for the current period you can use corresponding function applied to the curdate() function, so, no need to explicitly set current month number