php - in INSERT ON DUPLICATE KEY UPDATE, detect whether or not default value is used when specifying insert values -
suppose have query (in conjunction php)
insert table (field1, field2, field3) values (a, $field2, c) on duplicate key update field2 = if(values(field2) [ends using default(field2)], field2, values(field2))
where $field2 can either be
a. legitimate normal value inserted field2 or b. string "default(field2)" insert whatever mysql default value of field (suppose default 22)
now goal create on update part of query:
if case a.) occurred update column user specified $field2 value accordingly. othwerwise, if case b.) occurred maintain row (ie. don't modify data)
basically need fill in "[ends using default(field2)]" part of query accomplish above
now first impulse perform on duplicate key update field2 = if(values(field2) = default(field2), field2, values(field2))
but ends keeping row case a.) in $field2 = 22 since 22 default value of mysql hence values(field2) = default(field2) true despite fact in case a.) want 22 updated accordingly instead of keeping current value
in other words, how modify query it'll perform update if value explicitly inserted in, don't perform update if "default(field2)" ends beingness used
you can run mysql in strict mode , set column not null
definition. instead of trying insert default value column when case triggered, can insert null
. cause error, causing query fail.
i think bit hacky though. need understand utilize case improve see if should using different phone call pattern altogether.
php mysql sql sql-update insert-update
No comments:
Post a Comment