Mysql Triggers for timestamps -


i have mysql table several columns : id, starttime (timestamp), endtime (timestamp), status.

i add triggers :

  • automatically set timestamp 'starttime' field when new entry created
  • automatically set timestamp 'endtime' field when corresponding line 'status' set "closed"

newbie in mysql, have difficulties declaring these triggers...

thank help,

here complete solution : (for drupal use)

trigger 1 :

   $slqquery = 'create trigger `trigger_name` ';    $slqquery .= 'before insert on `table_name` ';    $slqquery .= 'for each row begin set new.starttime=now(); end;';    $query = db_query($slqquery); 

trigger 2 :

   $slqquery = 'create trigger `trigger_name` ';    $slqquery .= 'before update on `table_name` ';    $slqquery .= 'for each row begin ';    $slqquery .= "if (!(old.column_name <=> new.column_name) , (new.column_name <=> 'whatever')) ";    $slqquery .= 'set new.endtime=now(); ';    $slqquery .= 'end if; end;';    $query = db_query($slqquery);   

if can help.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -