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;
Related
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.
I'm trying to filter a SELECT query between NOW() and NOW - interval 10 minute(?), but i can't seem to get this to work, and it's given me a few questions on the topic.
I've looked through some documentation online, and alot of questions on stackoverflow but non of the solutions give me what i need. Looking at the TIMEDIFF and TIMESTAMPDIFF documentation, i only see it used like this;
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
However i don't want to just select the time difference, i want to use it in a query as a WHERE clause, something like;
SELECT * FROM tableName WHERE (the time difference betweeen NOW() and the stored timestamp is less than x minutes);
Is there a particular data type i need to set my column to?
How can i use the TIMEDIFF / TIMESTAMPDIFF correctly, and if these are not the correct methods i should be using, what is?
SELECT * FROM tableName WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) < 10
SELECT * FROM tableName
WHERE now() - interval 10 minute < stored_timestamp
Query 1 works but query 2 doesn't:
Query #1:
SELECT * FROM `users` WHERE users.dob <= '1994-1-14' AND users.dob >= '1993-1-14' LIMIT 10
Query #2:
SELECT * FROM `users` WHERE users.dob BETWEEN '1994-1-14' AND '1993-1-14' LIMIT 10
The 2nd one should be able to do the same thing as the first but I don't understand why it's not working.
The dob (date of birth) field in the users table is a type date field with records that look like this:
1988-11-08
1967-11-14
1991-03-09
1958-03-08
1967-06-30
1988-10-19
1986-01-23
1965-09-20
YEAR - MONTH - DAY
With either query #1 or #2 I'm trying to get back all users who are between 18 and 19 years of age, because 1994-1-14 is exactly 18 years from today and 1993-1-14 is 19 years from today. So is there a way to get the between query to work?
By not working I mean it doesn't return any records from the db while the working query does.
Also is the between query more efficient or is the performance difference negligible?
To answer the first part: "expr BETWEEN min AND max". Try switching those 2 dates in the second query.
The usage is wrong. See the BETWEEN documentation:
expr BETWEEN min AND max is equivalent to (min <= expr AND expr <= max).
Therefore, users.dob BETWEEN '1994-1-14' AND '1993-1-14' is the same as ('1994-1-14' <= users.dob AND users.dob <= '1993-1-14'), of which there will never be more than 0 results.
Simply reverse the order :)
There will be no performance difference when using either form, possibly subject to the note below. This transformation happens at the query planner level. However, if you have concerns, remember to profile, profile, profile. Then you can see for yourself and appease the premature-optimization demons.
Also note the ... note:
For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.
I have complicated query over very big table.
Long story short, when I use convert time to select period of day (let's say 12-13h, converting it from datetime row) query takes few minutes, instead of few seconds without convert!
So, I tried datepart, and it works well, almost instant, but, problem is, how to point to hours and minutes in same time?
Any other fast solution is more than welcome.
Thanks.
Meanwhile I came up with this:
DATEPART(HOUR, datetimecolumn)*100 + DATEPART(MINUTE, datetimecolumn)) between 1210 and 1540
You can use datePart if you are willing to do a bit of math, as shown below:
12:10 = 12 * 60 + 10 = 730 minutes
15:40 = 15 * 60 + 40 = 940 minutes
select * .....
where datepart(mi, datefield) between (12*60+10) and (15*60+40)
If you have a constant periods - i.e. - always hourly and no any floating periods - you may introduce something like "ordinal number of period" calculated field, index on it and query of it with precalculated period value
OR
is there are no any constant periods - try to calculate proper begin and end values prior to SELECT statement and use them in the query.
Keep in mind that using functions in where clause of query - sometimes is a bad idea. Using functions in ORDER BY clause - always bad
You can get GETTIME from following Function
alter function GetTimeOnly(#_DateTime DateTime)
returns datetime
as
begin
return dateadd(day, -datediff(day, 0, #_datetime), #_datetime)
end
go
OR YOU CAN HAVE THE TIME FROM CONVERT FUNCTION.
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly
I have a select query that retrieves all entries that are 3 weeks old.
I want to know if there is an expression like where date> Expr (CURDATE () - 3 WEEKS) or if I must first make calculations of differences in my php script.
The format of my date is a timestamp like that : 2010-06-21 16:59:59
Sincerely,
you can use the following line of code
where date>DATE_SUB(curdate(),INTERVAL 3 WEEK);
You can use the DATEDIFF() function like so: WHERE DATEDIFF(CURDATE(), date) = 21.
Use the actual input of timestamp is better,
as it allow query cache to take effect
From :- http://www.dangrossman.info/2007/04/26/mysql-tuning-disable-query-cache-on-frequently-updated-databases/
Queries that contain non-deterministic functions aren’t cached. That includes CURDATE(), RAND(), or any other function where the output isn’t always the same.
From documentation :- http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html