I am trying to write up a query that will give me the time since the last post in seconds, something along the lines of
SELECT (NOW() - mydatetime) as val1 FROM posts ORDER BY mydatetime DESC LIMIT 1
How do I get val1 in seconds?
Since you are only interested in seconds, you can simply subtract the two dates as timestamps:
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydatetime) FROM ...
Note: MySQL has a set of Date Time Functions, I encourage you to browse through them.
TIMESTAMPDIFF(SECOND, mydatetime, NOW())
Related
I have a column in my database which holds an expire time being inserted with NOW() + INTERVAL 30 MINUTE;
I need to be able to pull out minutes remaining from that column. I have no idea where to start.
Thanks in advance.
SELECT TIMESTAMPDIFF(MINUTE,NOW(),column1) AS minutes_remaining FROM table1;
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
Note that the order of the arguments is extremely counterintuitive... to get a positive number as a result, the larger (later) datetime value is the last argument. If the second argument is >= the third, the result will be <= 0.
I have a simple table that has a TIME column named timeC in the example below. I want to select all the records for which timeC is in the last five minutes. I have tried the following and many variations without success.
SELECT * FROM sample_schema.`exampleTable`
WHERE MINUTE(TIMEDIFF(SELECT TIME(NOW())), `timeC`)<5;
Note that the column is TIME, not DATETIME or TIMESTAMP.
Suggestions?
Your parentheses are in the wrong places. So you're calling TIMEDIFF() with 1 argument, and calling MINUTE() with 2 arguments. Also, you don't need to use SELECT to get TIME(NOW()), you can just use that function call as an argument.
SELECT *
FROM exampleTable
WHERE MINUTE(TIMEDIFF(TIME(NOW()), timeC)) < 5
However, there's still a problem with this. TIMEDIFF() can return a negative time if timeC is later in the day, but MINUTE() always returns the positive value of the minute, so this will match anything from 5 minutes ago to 5 minutes later. It would be better to simply compare the time with a range:
SELECT *
FROM exampleTable
WHERE timeC BETWEEN TIME(DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AND TIME(NOW())
There's one ramaining problem. If you perform this query shortly after midnight, the time of 5 minutes earlier will be a late time from the previous day. For instance, if it's currently 00:02, 5 minutes earlier will be 23:57, and nothing will match the BETWEEN expression. You need to check for that:
SELECT *
FROM exampleTable
WHERE CASE
WHEN TIME(NOW()) >= '00:05'
THEN timeC BETWEEN TIME(DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AND TIME(NOW())
ELSE timeC BETWEEN '00:00' AND TIME(NOW())
OR
timeC BETWEEN TIME(DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AND '23:59:59'
END
I am using version 14.4 of mysql and I am trying to execute the following query:
SELECT TIMESTAMPDIFF(MINUTE, MINUTE(NOW()), NOW())
This should return a timestamp that is on the current hour, but it's always returning null. TIMESTAMPADD works just fine, I am only having trouble with this function. I have looked for answers to this problem through google and mysql documentation but I couldn't find anything.
Does anyone have any an idea on what's going wrong?
My full goal is a query that returns how many minutes are left from now until 5 of the next hour. For example. If its 1:30, our target time is 1:55, so the query would return 25
If you want the hour
SELECT CURTIME()
That query you posted is absurd,or be more clear about what you want.
I believe you're trying to floor the current UNIX timestamp to the current hour? If so, why not be explicit about it?
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP() % 3600;
Based on your edits, you can also calculate the difference to the 5 minute mark preceding the next hour as follows:
SELECT MINUTE( TIMEDIFF(
NOW(),
FROM_UNIXTIME( UNIX_TIMESTAMP() - UNIX_TIMESTAMP() % 3600 + 3600 - 5*60 )
) );
This is just a proof-of-concept query; I haven't yet accounted for rounding the current time to the minute (because I don't know if you want to go up or down), nor handled the edge case where the current time is within 5 minutes of the next hour already (e.g. 1:59).
I am trying to write an SQL query to return how many links were submitted to my website over the last 7 day period. So far I have this:
SELECT COUNT(`id`) AS `count`
FROM `links`
WHERE `created` > NOW() - 86400
AND `created` < NOW()
this works for one day, it returns one row called count with the number of links submitted in the last 24 hours. I need to change it to return 2 columns called date and count, with 7 rows (one for each day).
The tricky part that I can't get my head around is that created is a timestamp column, and I don't have access to change it so I have to work with it.
Edit: work in progress for the query:
SELECT DAY(FROM_UNIXTIME(created)) AS day, COUNT(id) count
FROM links
GROUP BY DAY(FROM_UNIXTIME(created))
LIMIT 7
NOW() actually shouldn't be working as it returns a datetime. Also, if you want to fetch 7 days worth of data, you want to subtract 604800 from UNIX_TIMESTAMP(). You can use then date and time functions with FROM_UNIXTIME. This will make grouping easier. Optimally, your column should be of datetime type.
It would go something like:
SELECT DAY(FROM_UNIXTIME(created)) day, COUNT(id) count
FROM links
WHERE created > UNIX_TIMESTAMP() - 604800 AND created < UNIX_TIMESTAMP()
GROUP BY DAY(FROM_UNIXTIME(created))
You can alternatively use the BETWEEN operator:
WHERE created BETWEEN UNIX_TIMESTAMP() - 604800 AND UNIX_TIMESTAMP()
See the demo
I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.