mysql - Check for maximum amount of records in a period -
i've got table "date" column (timestamp). i'm trying achieve check if after inserting row there no more 3 records contained in single 24 hours period, example:
i have records following dates:
1. 2015-05-31 23:14:00 2. 2015-06-01 02:07:00 3. 2015-06-01 15:16:00
so shouldn't able to insert row date of (for example) 2015-06-01 16:01:00
or 2015-06-01 01:01:00
should able add records dates of (for example): 2015-06-01 23:50:00
, 2015-05-31 01:05:00
.
how can achieve this?
there little trick can achieve problem purely sql
set @date = '2015-05-31 1:14:00'; insert tbldate(inputdate) select @date ( ( select count(*) c tbldate t1 inner join tbldate t2 t1.inputdate <= t2.inputdate , t2.inputdate <= t1.inputdate + interval 24 hour , t1.inputdate between @date - interval 24 hour , @date group t1.inputdate ) union (select 0 c) ) r having max(r.c) < 2
where @date date want insert.
Comments
Post a Comment