I want to create a trigger MySQL that updates two fields at the same time.
Whenever a record is added or inserted, I want the contents to be copied to one field or the other, depending on which one is empty/edited.
due_date
is a varchar
and end_date
is a datetime
.
e.g.
- IF new record with
due_date
filled in butend_date
empty → copydue_date
toend_date
after insert. - IF new record with
end_date
filled in butdue_date
empty → copyend_date
todue_date
after insert. - IF update to
due_date
→ copy that toend_date
after update - IF update to
end_date
→ copy that todue_date
after update
Please note, the due_date
format is already the correct and same format as datetime.
2015-11-16 15:11:50
for both fields. Also the reason is: If on one page users edit theend_date
then it needs to be copied to thedue_date
and if another user on a different page updates thedue_date
it needs to be copied to theend_date
that's it! – mesqueeb Jan 5 '16 at 6:45sql
is a language which is very, very different from SQL Server the database engine from Microsoft). Are you using SQL Server? MySQL? Oracle? Why woulddue_date
be avarchar
rather than adatetime
? If your end goal is that both columns have the same data, why do you have two separate columns? What if anupdate
statement changes both columns? Which value wins? – Justin Cave Jan 5 '16 at 7:01