I am using on duplicate key to update data in a mySQL DB v 8.0.35 (AWS RDS instance)
I want to update a selection of fields with the new value. Plus set a further field (up[dated) to 1 only if a specific field (templateURL) has changed. This should be left at the old value (0) if only other fields have changed.
I believe I ought to be able to use IF or CASE to achieve this based on previous posts to similar questions. However, it looks like this used to work with the old values(field) syntax, bit does not work with the new alias syntax.
I am getting an "You have an error in your SQL" error when running this SQL:
insert into data_manager.rars_CoachTypes c
(coach_type,template_height,template_width,seat_height,seat_width,toc,templateURL,serviceId,latest_depart_date,our_template_width,our_template_height,features,imported) values ? as INSERTDATA
on duplicate key update
template_height=INSERTDATA.template_height,
template_width=INSERTDATA.template_width,
seat_height=INSERTDATA.seat_height,
seat_width=INSERTDATA.seat_width,
templateURL=INSERTDATA.templateURL,
latest_depart_date=INSERTDATA.latest_depart_date,
updated = IF (c.templateURL<>INSERTDATA.templateURL,1,0)
I've also tried it with CASE rather than IF, but the result is the same.
updated = CASE WHEN c.templateURL<>INSERTDATA.templateURL THEN 1 ELSE 0 END
I added the alias "c" because I was previously getting another error saying the field templateURL was ambiguous... Not sure why it should think that?
For mysql your syntax is very off
see manual
it mus be
or as Barmar correctly said, you can use use
OLD
andNEW
to differentiate both values