0

I was trying to update a column value that has a null value.

Table:

+------------+-----------+-------+
| product_no | name      | price |
+------------+-----------+-------+
|          1 | Cheese    |  9.99 |
|       NULL | Meat      | 17.00 |
|          2 | Pepperoni |  NULL |
+------------+-----------+-------+

Update:

UPDATE products SET product_no = 6 WHERE product_no = NULL;

Output:

Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

Table definition:

+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| product_no | int(11)       | YES  |     | NULL    |       |
| name       | varchar(255)  | YES  |     | NULL    |       |
| price      | decimal(10,2) | YES  |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+

Why is this not updating to 6?

3
  • Nothing is equal, nor not equal, to NULL - not even NULL! A value either IS or IS NOT NULL. Commented Jul 14, 2015 at 0:43
  • Ooh forgot about that. Commented Jul 14, 2015 at 0:45
  • select null=null gives a nice answer Commented Jul 14, 2015 at 0:47

3 Answers 3

1

Try this:

UPDATE products SET product_no = 6 WHERE product_no is NULL;
Sign up to request clarification or add additional context in comments.

Comments

0

NULL value is special, use:

WHERE product_no IS NULL

or

WHERE product_no <=> NULL

Comments

0

The reason is because NULL is special.

MySql states:

To test for NULL, use the IS NULL and IS NOT NULL operators...

You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.

Hence:

UPDATE products SET product_no = 6 WHERE product_no IS NULL;

Comments

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.