Time difference in mysql - mysql

I want to get the records from mysql data base based on time.
I have lastquery_date column in my table every time update the date and time in that column.
I want to get time difference with current date is less than 12 mins
How can i get this query..
Please help me

see the link below and develop your own logic :)
TIMEDIFF

Your question is not entirely clear, but I think this is what you want:
SELECT *
FROM your_table
WHERE lastquery_date >= NOW() - INTERVAL 12 MINUTE

Related

SQL how can i get previous minute?

I have a data table whose structure looks like this.
date time order_id action quantity
How can I query to know how many entries are made to this table previous minute?
Say suppose the time now is 14:46 I want to know how many row entries were made to this table at 14:45. how can I do that?
The problem I am facing now is that I don't know how can I get last minute's time stamp. CURRENT_TIMESTAMP() is giving me correct current time. But I tried CURRENT_TIMESTAMP()-1 which gives some decimal number.
you can use DATE_SUB(), specifying a date and an INTERVAL as documented for DATE_ADD(). Example:
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
You can use my query to find the last minute :
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);

Fetch mysql data of timstamp depending on time given

I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.

MySQL date and hour issue

I have read that MySQL has some issues regarding date type. I want to get a date interval taking into account the hour. My question is can I do a between query, or make a column for the hour as well?
P.S:
select * from main where main.date between 01-06-11 07:59 and 01-06-11 15:59

How to select the matching details from table mysql query equal to current date

I have read the topic Mysql query: retrieve current date query ., but my question is how can i select the row in a particular given time span.
i.e if my date field is having the entry as 2010:10:27 10:30:00 then that row should be selected when the current time reaches 2010:10:27 10:00 and until the current time reaches the 2010:10:27 18:30:00 . like that. Any help on this would do a great benefits for me..
thanks in advance
Have you checked out the BETWEEN operator? I think it will be helpful to you. Maybe something like this:
WHERE
field
BETWEEN
DATE_SUB(NOW(), INTERVAL 8 HOURS)
AND
DATE_ADD(NOW(), INTERVAL 30 MINUTES)

How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?

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.