The update
statement can refer to values in the from
clause of insert . . . select
. The idea is to take your query and make it a subquery. Then add one more column which is your calculated column. You can use this in the set
clause:
insert into table(col1, . . .)
select . . .
from (select cols, . . .,
col1 in (complicated stuff here) as IsPresent
from . . .
) t
on duplicate key update colx = if(t.IsPresent, a, b), coly(if.tIsPresent, d, e);
Of course, you may be able to get rid of the subquery (which has its own overhead), but this is the most general approach from your description.