This is my first time using mysql and I am tying to learn how to use trigger.
I have two tables: lpr_upload and lpr_traveltime.
my lpr_upload table look like this
+----------+---------+--------+-----------+
| date_time | site_id | lane_id| lpr_text |
+----------+---------+--------+-----------+
Whenever I add a new row into lpr_upload, i want to check if lpr_tex of the new row already exist or not in the table. If it already exist, i want to add a new row into lpr_traveltime with lpr_text and new datetime – old datetime. This is what i have so far:
insert into lpr_opt_travel_times(travel_time, lpr_text)
value (select TIMEDIFF (new.date_time, …) , lpr_text) on duplicate key update …
I dont know how to finish this. also, how would I get the date_time from the row that already exist in the table?
Try this:
Declare a var with date time datatype as I assume the column date_time is datetime type.
Select from NEW magic table the value of date_time for the inserted lpr_text but whose date_time is different from the inserted one.
If the var is null meaning no assignment took place and the var retains the initial default value of NULL then insert the row in your other table using the var and the inserted lpr_text