I re-written the complete question because I was not clear enough the first time.
I am trying to update a given row id in users table and fill only values that are null, like this:
UPDATE USERS SET
address='Main Street' IF current address IS NULL
gender='M' AND full_name='JOHN SMITH' IF both gender and full_name ARE NULL
job='Mechanic' IF current job is NULL
WHERE id=123
I don't know how to do the part with updateing both gender and full_name, I want the other fields to still trigger and update individually regardless of the gender
and full_name
values
The if
in update
statement in SQL
gets into the WHERE
part of the query:
UPDATE table_name SET gender = $gender WHERE gender IS NULL AND full_name IS NULL
I would add also
AND id = $id_you_want_to_update
This might be a verbatim answer to your question:
UPDATE yourTable
SET gender = $gender,
full_name = $full_name
WHERE gender IS NULL AND full_name IS NULL
However, it seems to make more sense to update a record if either the gender or full name be NULL
. In this case, you can try:
UPDATE yourTable
SET gender = COALESCE(gender, $gender),
full_name = COALESCE(full_name, $full_name)
WHERE gender IS NULL OR full_name IS NULL