Jump to content

Update IF NULL


Rusty3

Recommended Posts

Hello. Any way to do this on a single Update query? I mean I have rows with many many columns and would like to update only the values that are NULL. Can I use IFNULL?

 

var1 = NULL

var2 = C

 

UPDATE myvariables SET var1=A, var2=B (...) WHERE id=100

 

I want var1 to be updated, but var2 to be left intact.

 

I am sure this is recurring problem.

 

Thanks for any clue.

Link to comment
https://forums.phpfreaks.com/topic/211733-update-if-null/
Share on other sites

Hi

 

Not sure as never tried it on an update, but would think you could use a switch statement

 

Following isn't tested but might give you an idea.

 

UPDATE myvariables 
SET var1 = CASE var1 WHEN NULL THEN A ELSE VAR1 END,
var2 = CASE var2 WHEN NULL THEN B ELSE VAR2 END
WHERE id=100

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/211733-update-if-null/#findComment-1103710
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.