I have a situation where a user may or may not provide various bits of data that need to go into a row containing that user’s info. The issue is that they need to make multiple entries into the row, over time, and I don’t want to null out any pre-existing values, if those had been set in the past.
My desire is to do something that looks (in pseudo code) like this:
var a,b,c;// these are the values that the user provides any of which may be null at any time
update user if a != null set name = a, if b != null set phone = b, if c != null set email c
I’ve been trying to fashion an IF to work in this manner but I get syntax errors with the various ways I’ve tried. Hoping someone can shed light on the right way to do this (if it’s possible).
TIA
One way to do this is to “default” each column to its original value with COALESCE().
COALESCE() returns its first non-null argument, so if
ais null, it’ll just return the original value ofname, so there will be no net change.MySQL also supports a function ISNULL() which works like COALESCE(), but COALESCE() is standard SQL. Also, COALESCE() supports more than two arguments.