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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.