how to get minutes from two different date in mysql - mysql

I am having fields called 'timestamp','onlineuser' from table called 'User'. I need to select onlineuser who is having the timestamp which is less than 5 minutes between NOW(),timestamp.
For example, timestamp will be like 2011-06-20 16:02:22.
I tried with mysql query , but seems wrong. kindly help me

SELECT * FROM user WHERE timestamp >= NOW() - INTERVAL 5 MINUTE

Related

How to show last 10 min old records from mysql

I have a MySQL database having column time containing Unix timestamp like 1482858013.
Now I want to make a MySQL query which should show only the last five minute old records from the database. How can I do this? Thanks in advance.
Assuming that your table is called ata_table, then query would be something like:
select
*
from data_table
where
`time` > unix_timestamp()-300;
Use DATE_SUB() to get the time 5 minutes ago, and UNIX_TIMSTAMP() to convert that to a numeric timestamp.
WHERE time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 MINUTE))
SELECT FROM_UNIXTIME(timestamp/1000,'%Y-%M-%d) as timestamp FROM table
WHERE timestamp > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 MINUTE))
Order by timestamp desc

How to query data for a certain data range in MySQL

We are using MySQL, and the table have a column named 'createdat` which means the time when the record are inserted.
Now we want to get data within 10 days from its created time.
Something like this:
select xx from table where time_of_now - createdat<= 10
I wonder if MySQL support this kind of query?
You can use DATEDIFF to get the difference between 2 dates in days, like so:
select xx
from table
where datediff(now(),createdat) <= 10
Documentation
select xx from table where createdat >= CURDATE() - INTERVAL 10 day

mysql - setting datetime field to only a day or certain hour

In MYSQL,
I have two datetime fields. One for the actual date which I know how it works.
And the other I would like to hold the interval. For example 5 hours or 5 days. How would I do that ?
if understand rightly, use the INTERVAL
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 5 DAY) <= date_col;
Refer this link: OfficalSite

How can I get the date difference of a timestamp

I am trying to create a query that will limit insertion into a table based on the last time the poster sent data to the table.
For example if you posted data to the table then you are locked out of the system for another 10 hours. Here is what I came up with so far. But I get nowhere with the actual results on the data. Any help?
SELECT DATE( `date` )
FROM tablename
WHERE DATE( CURDATE( ) ) < CURDATE( ) - INTERVAL 1002
DAY
LIMIT 0 , 30
This will return a single post from the last 10 hours, if it exists:
SELECT *
FROM tablename
WHERE `date` >= NOW() - INTERVAL 10 HOUR
LIMIT 1
I'm assuming date is declared as DATETIME, since actual DATE does not contain the time part and hence is only day-accurate.
If date is an integer UNIX timestamp, use this:
SELECT *
FROM tablename
WHERE `date` >= UNIX_TIMESTAMP(NOW() - INTERVAL 10 HOUR)
LIMIT 1
There are a number of ways you could do this. Perhaps if you have a user settings table you could simply add a "last_insert" field, and store the timestamp as an integer value- that would be a super simple way to do it- you could check the current timestamp vs user_settings.last_insert and voila!
I suppose you could use datetime too. Whatever floats the boat.
First of all, you need a DATETIME column and not a DATE column. Assuming that tablename.date is a DATETIME column, then 10 hours before right now is CURRENT_TIMESTAMP - INTERVAL 10 HOUR.
First of all create a Time (TIMESTAMP DEFAULT CURRENT_TIMESTAMP) columnt in your table. It will be automatically set to current date on row insert
Then check:
SELECT COUNT(*) FROM Table WHERE Time > NOW() - INTERVAL 10 HOUR
If its 1 or more - block
You must compare the time last post was put with current time, not current time with current time :|

Grouping timestamp field by date

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