views:

21

answers:

1

I have a table T that has a column A. A started with a default of NULL. 40 rows later, I changed the default to 1. Three rows had a value of 2. I tried to select all the rows where column A where not 2 and set them to the new default of 1 (which hadn't happened automatically when I altered the table). I first tried:

update T set A=1 where A != 2;

Nada. Didn't work. Selected zero rows. Next I tried:

update T set A=1 where !(A=2);

Nope, nothing there either. I tried plugging them into selects, to see if there was something wrong with the update, but those returned nothing either. The MySQL reference manual says that != and ! are valid operators and ought to be perfectly valid in that context. I finally achieved my goal using IS NULL, but those statements should have worked. So what gives? Why didn't that work?

I am running MySQL version: 5.1.41-3ubuntu12.6 (Ubuntu)

+1  A: 

Try:

update T set A=1 where A != 2 or A is null; 

Comparisons involving NULL evaluate to NULL(UNKNOWN) and will thus never be true.

RedFilter
Mentioned in the question that I already achieved my goal using IS NULL. Meaning, I already did that.But thanks for the bit about comparisons involving NULL!
Daniel Bingham