简体   繁体   中英

null value not found in database

I have an InnoDB table with a column called 'status'. This can be filled with either 'succes' or 'special', or with NULL, when the code ran an exception.

I have a cronjob which looks at this table and tries to reprocess rows with a status other than 'succes' or 'special'. I noticed however, that it doesn't work.

So I queried the table with the following query:

SELECT * FROM master_innodb.some_table WHERE status != 'succes' AND status != 'special';

It returns 0 rows.

When I changed the row that I expected it to return from status NULL to an empty field, the query returned this row.

Can someone explain this?

Regards,

Matthijs

So I think reckon you could do with understanding a little bit more about what a NULL value represents in a MySQL database. NULL can be a confusing beast at the best of times. From the MySQL docs :

The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.

So NULL pretty much means 'an absence of data'. So when you specify a predicate of:

status != 'success' AND status != 'special'

Then the row with NULL in the status column is not returned because we don't know whether or not the row with the NULL status is equal to 'succes' or equal to 'special'. Hence it is not returned.

You could try this instead the get rows to reprocess in your CRON job:

SELECT * FROM master_innodb.some_table WHERE status is null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM