I have a MySQL Table with one datetime column. I want to prevent that the PHP-script gets to much data. So i'm searching for a solution that a MySql query only selects rows which have a distance of 1 minute or whatever. is there something simple or do i have to code a for-loop with a new mysql query every time.
Example
timestamp
2012-09-25 00:00:00-->
2012-09-25 00:00:50
2012-09-25 00:01:23
2012-09-25 00:01:30-->
2012-09-25 00:02:33
2012-09-25 00:02:40
2012-09-25 00:03:01-->i want those
thanks in advance
Try this :
SELECT create_time
FROM timeTable
WHERE create_time
IN (
SELECT min( create_time )
FROM timeTable
GROUP BY FROM_UNIXTIME( UNIX_TIMESTAMP( create_time ) - MOD( UNIX_TIMESTAMP( create_time ) , 60 ) );
How it works :
i) Groups the table by datetime rounded to the interval, 1 minute (60 seconds) here.
ii) Gets the top row from each group.
This can be a good sampling criteria for your data.
This query can be optimized alot on these points:
i) Put a where clause for a date = REQUIRED DATE, and then do other operations on hour+minutes instead of whole datetime.
ii) If your interval is 1 minute, then substring of the timestamp or date_format can be tried too to round it off to nearest minute.
eg.
SELECT create_time
FROM timeTable
WHERE create_time
IN (
SELECT min( create_time )
FROM timeTable
GROUP BY DATE_FORMAT( `create_time` , 'Y-M-D %H:%i' )
);
Try this
SET #time := '1000-01-01 00:00:00';
SET #interval := 60;
SELECT colDate
FROM table
WHERE TIMESTAMPDIFF( SECOND, #time, colDate ) >= #interval
AND #time := colDate
How it works.
#interval is the time difference desired between the current and previous colDate. The first parameter in TIMESTAMPDIFF determines the unit of time that the interval will use. ex: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
#time keeps track of the previous colDate, and it is compared with the current row. If the difference between the previous and current colDate is equal to or greater than the interval, it is included.
WHERE timestamp LIKE '%:30:00%' will get you every 30 seconds..
But this will only work if you have uniform entries..if your timestamps dont all end evenly.. you'll need to let us know.
EDIT
I think you may be looking for this:
How do you select every n-th row from mysql
Related
Query to get list of records between stating time and end time
Ex:
Start_time End_time
----------------------
22:00 05:00
20:00 02:00
If i query in between the time have to get those result am not using any date specific. i used Now() Between not worth any one have good idea
Try to use DATE_FORMAT(dt,'%H:%i') to get HH:MI string from date and then compare it with start_time and end_time depend on start_time>end_time or not.
SET #start_time = '22:00';
SET #end_time = '05:00';
select *
from T
where (
(#start_time<=#end_time)
AND (DATE_FORMAT(dt,'%H:%i') >= #start_time)
AND (DATE_FORMAT(dt,'%H:%i') <= #end_time)
)
OR
( (#start_time>#end_time)
AND
(
(DATE_FORMAT(dt,'%H:%i') >= #start_time)
OR
(DATE_FORMAT(dt,'%H:%i') <= #end_time)
)
);
SQLFiddle demo
Suppose I have a table containing a month's transaction data with transaction_time stored in a DATETIME field.
I want to get all the transactions that occurred between 12:00:00 and 13:00:00, irrespective of the day. WHERE transaction_time BETWEEN x AND y would have to be date-specific, but I need that same time period of all dates.
How can I filter for such a range in a MySQL query?
You can filter on the result of applying MySQL's HOUR() function to your DATETIME value:
WHERE HOUR(transaction_time) = 12
If you need to filter across more exact time ranges, you could convert the times to seconds as follows:
WHERE TIME_TO_SEC(TIME(transaction_time)) BETWEEN TIME_TO_SEC('12:00:00')
AND TIME_TO_SEC('13:00:00')
You can use:
select * from transactions
where convert(time, transaction_time) between '12:00:00' and '13:00:00'
for MSSQL and:
select * from transactions
where extract(hour_second from transaction_time) between 120000 and 130000
for MySql.
You have two time constraints. The first is to restrict the dates to a particular month. Assume we get the first day of the month as a date parameter:
where transaction_time >= :searchMonth
and transaction_time < Date_Add( :searchMonth, interval 1 month )
Now we're only looking at rows from that month. Now limit it to the hour specified. Assume we get the hour as an integer parameter:
and extract( hour from transaction_time ) between :hr and (:hr + 1)
Now that final part is based on your own code. Let me say that when the requirements read "during a particular hour of the day" and the request reads "during the noon hour" then I am wont to write it like this:
and extract( hour from transaction_time ) >= :hr
and extract( hour from transaction_time ) < (:hr + 1)
because hour 13 (1PM) is the first click of the next hour. So if there is a transaction time at exactly 13:00:00 and you use between, then it will show up when looking at the noon hour and also when looking at the 1PM hour. That is NOT generally a desired result. You may want to verify that with your analyst.
So the complete filter, the way I would write it, is this:
where transaction_time >= :searchMonth
and transaction_time < Date_Add( :searchMonth, interval 1 month )
and extract( hour from transaction_time ) >= :hr
and extract( hour from transaction_time ) < (:hr + 1)
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 :|
I have data stored in a mySQL database and I want to retrieve the rows that have been inserted between now and the beginning of the current hour: not in the last hour, since the start of the hour. For example, at 9:16 I want the rows from 9:00 until now. My time is stored in datatime format. How can I do this?
The query below selects all rows that are between the current hour and current time. So if current time is 10:11 it will select all rows between 10:00 and 10:11 within current date.
SELECT *
FROM Test
WHERE mytime BETWEEN DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00')
AND DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')
I Tested it and worked.
The first thing that comes to mind is:
select *
from t
where date(now() ) = date(t.timeval) and
hour(now() ) = hour(t.timeval)
I made the assumption that you don't have future records in the data. If so, then this comes to mind:
select *
from t
where date(now() ) = date(t.timeval) and
hour(now() ) = hour(t.timeval) and
timeval < now()
SELECT * FROM times WHERE t >= DATE_FORMAT(NOW(),'%Y-%m-%d %H:00:00');
I need to add 12 hours to a MySQL TIME field (not DATETIME) and I'm having trouble.
UPDATE `events`
SET start_time = DATE_ADD(start_time, INTERVAL 12 HOUR)
WHERE `start_time` < '11:00:00'
returns with no errors but doesn't change anything, I think because start_time is a TIME field.
UPDATE `events`
SET start_time = start_time + '12:00:00'
WHERE `start_time` < '11:00:00'
adds 12 seconds.
Try using ADDTIME instead of DATE_ADD. You could do SET start_time = ADDTIME(start_time, '12:00:00')
UPDATE `events`
SET start_time = start_time + INTERVAL 12 HOUR
WHERE `start_time` < '11:00:00'
The MySQL functions that accept INTERVAL arguments are mostly unnecessary; you can just add and subtract intervals with + and -.
set start_time = ADDTIME(start_time,'12:00:00')
DATE_ADD works fine with timestamp etc, but not with TIME
update my_table SET modified_date = ADDTIME(scheduled_date, '03:15:00')
This will add 3 hours , 15 minutes in modified_date
if the developer does not want to update data and wants to add hours or minutes to time. It can be done following way:
The developer can use an AddTime() function to add hours to time column in MySQL.
It can be used like below way:
AddTime(COLUMN,’01:00:00′) to add an hour in MySQL time column
AddTime(COLUMN,’00:01:00′) to add a minute in MySQL time column
sql query example:
select id,name,AddTime(login_time,'01:00:00') as login_time FROM `table`
First answer:
SET start_time = ADDTIME(start_time, '12:00:00')
Will only work if start_time is less than 12 hours.
If start_time is for example 13:00:00, then the end result will be 25:00:00, to get 01:00:00, you can use the following trick:
SET start_time = DATE_FORMAT(ADDTIME(CONCAT('1970-01-01 ', start_time), '12:00:00'), '%H:%i:%s')
(I used this to correct for the timezone)