datetime - MySQL date_add and repeat forever -


i got alert table users, in have send alerts users in user defined intervals 0 ( once), 3 months, 6 months, 1 year

so designed table

id   |   user_id    |   alert_date            |  repeat_int -----+--------------+-------------------------+------------- 12   |    747       |   2013-04-19 00:00:00   |    0 13   |    746       |   2013-03-19 00:00:00   |    1 14   |    745       |   2012-04-19 00:00:00   |    0 15   |    744       |   2013-04-19 00:00:00   |    0 16   |    743       |   2013-05-19 00:00:00   |    0 

we sending alert day before "alert_date"

with following query can fetch data

select al.id,        al.user_id,        al.alert_date,        al.repeat_int repunit alerts al date_add(alert_date,interval repeat_int month)=date_add(current_date,interval 1 day)   or date(al.alert_date)=date_add(current_date,interval 1 day) 

its working file real problem

the repeat works once, need repeat every interval

ie. if alert date 2012-03-14 , repeat_int 0 - need work once if alert date 2012-03-14 , repeat_int 1 - need work in every 14th 2012-03-14

and if alert date 2012-03-14 , repeat_int 3 - need work in every 3 month's 14. ie alert on 2012-03-14, 2012-06-14, 2012-09-14 etc...

is there idea / way ? in advance

update

the op has changed schema in response comments, query essentially:

select *   alerts  current_date + interval 1 day = coalesce(next_alert_date, alert_date); 

this handles "next_alert_date" being null on first run.

original answer

for original schema:

select *   alerts   join (select current_date + interval 1 day tomorrow) d  -- want alert if        --  1. tomorrow alert_date        tomorrow = alert_date        or        --        --  2. tomorrow "repeat_int" months removed alert_date, falling on        --     same day of month or on end of month if original        --     alert_date day of month later in month possible        --     now.  e.g., 2013-01-31 repeated monthly adjusted 2013-02-28.        (          period_diff(date_format(tomorrow, '%y%m'), date_format(alert_date, '%y%m'))            mod repeat_int = 0          ,          -- make sure @ same day of month          ( (dayofmonth(tomorrow) = dayofmonth(alert_date)           or          -- or, if day of alert beyond last day of our month,          -- @ end of our month.            (last_day(alert_date) > last_day(tomorrow)               ,             dayofmonth(tomorrow) = last_day(tomorrow)) )         ); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -