简体   繁体   English

在数据库中找不到空值

[英]null value not found in database

I have an InnoDB table with a column called 'status'. 我有一个带有名为“状态”的列的InnoDB表。 This can be filled with either 'succes' or 'special', or with NULL, when the code ran an exception. 当代码运行异常时,可以用'succes'或'special'或NULL填充。

I have a cronjob which looks at this table and tries to reprocess rows with a status other than 'succes' or 'special'. 我有一个cronjob,它查看此表并尝试重新处理状态为“成功”或“特殊”以外的行。 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'; SELECT * FROM master_innodb.some_table WHERE status !='成功'AND status !='特殊';

It returns 0 rows. 它返回0行。

When I changed the row that I expected it to return from status NULL to an empty field, the query returned this row. 当我将期望其从状态NULL返回的行更改为空字段时,查询将返回此行。

Can someone explain this? 有人可以解释吗?

Regards, 问候,

Matthijs 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值在MySQL数据库中表示的内容。 NULL can be a confusing beast at the best of times. 在最好的情况下,NULL可能是令人困惑的野兽。 From the MySQL docs : MySQL文档

The NULL value can be surprising until you get used to it. 在您习惯之前,NULL值可能令人惊讶。 Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values. 从概念上讲,NULL表示“缺少未知值”,并且与其他值的处理方式有所不同。

So NULL pretty much means 'an absence of data'. 因此,NULL几乎意味着“没有数据”。 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'. 然后,由于我们不知道status为NULL的行是等于“成功”还是等于“特殊”,所以不返回status列中具有NULL的行。 Hence it is not returned. 因此,它不会返回。

You could try this instead the get rows to reprocess in your CRON job: 您可以尝试以下操作,而不是在CRON作业中获取要重新处理的行:

SELECT * FROM master_innodb.some_table WHERE status is null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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