Problem with NULL values you should remember

When trying to execute SELECT OR UPDATE query for rows different than a specific value, we should be aware to insert additional statement for null values too.

For Example:

        UPDATE account_event 
        SET account_event.assigned_to_id = 2 
        WHERE account_event.status != 1

With this query your goal is to update all rows where account_event.status is different from 1. But if status have null value it wont be updated!
In MySQL all null elements are skipped from comparison and will never be Selected or Updated.

When you have a null fields you should always add additional statement for null like this:

        UPDATE account_event 
        SET account_event.assigned_to_id = 2 
        WHERE account_event.status != 1 
        OR account_event.status IS NULL

Leave a Reply

Your email address will not be published. Required fields are marked *