문제

This may be a silly question, but null seems to neither equal nor unequal any empty string "".

I have a table with the following values:

id field1 field2 field3
1   a        b     c
2   null     b     c
3            b     c
4   a        b     c

My query

select * from table where field1 = ""

does not return row 2 where the value of field1 is null. This makes absolute sense, as null is not equal to an empty string.

But,

select * from table where field1 != "" 

doesn't return the row 2 either.

Does anyone have an explanation for the historic origin of this? Is it because the value null means that we do not know the value and hence it is unknown whether field1 is equal or unequal to an empty string for row 2?

도움이 되었습니까?

해결책 2

Is it because the value null means that we do not know the value and hence it is unknown whether field1 is equal or unequal to an empty string for row 2?

You are correct. Whenever you perform a comparison against NULL, the result is NULL.

You can think of NULL as meaning "unknown". When NULL is stored in a column for a record, it doesn't mean that it doesn't have a value, it means it hasn't been entered in the database.

For example, you might have a "Person" record, with a "Date of Birth" column. If the value is NULL, it doesn't mean that person wasn't born. It just means it hasn't been entered, so, according to the database, that person's birth date is "unknown".

If you don't know the person's date of birth, you can't answer either of these questions:

Was the person born on April 1st?

Was the person not born on April 1st?

The answer to both is "unknown".

You also can't answer:

Was the person born after April 1st?

Was the person not born before April 1st?

Whenever you compare a known value against "unknown", the answer is going to be "unknown".

Further, if two people's dates of birth are both NULL or "unknown", you also can't answer these:

Were the two people born on the same day?

Were the two people not born on the same day?

Was person one born after person two?

Was person two born after person one?

Whenever you compare an "unknown" value to another "unknown" value, the answer is "unknown".

Comparing anything against an "unknown" value yields "unknown".

You can, however, always answer the following:

Do I know the person's date of birth?

Do I not know the person's date of birth?

To ask that question in MySQL, you use IS NOT NULL and IS NULL.

다른 팁

NULL is not equal to an empty. NULL is not equal to anything, including NULL. To compare to NULL you need to use IS NULL or IS NOT NULL

SELECT NULL = NULL,
NULL != NULL,
NULL IS NULL,
NULL IS NOT NULL

NULL = NULL     NULL != NULL        NULL IS NULL    NULL IS NOT NULL
(null)             (null)            1                  0

SQL Fiddle

select * from table where field1 IS NULL

or

select * from table where field1 IS NOT NULL
"" != null

As posted in the question, you have a null value in your database, however, you are checking it agains an empty string, which is not actually null.

In mysql, null comparisons are done using

IS NULL

For example:

SELECT * FROM table WHERE field1 IS NULL;

or

SELECT * FROM table WHERE field1 IS NOT NULL;

The special NULL value is intended to neither match (via the = operator) any actual value nor not match (via the <> operator) that value. In fact, it doesn't even match or not match itself. Therefore to fetch a result set containing rows where a certain field is NULL is done as follows:

SELECT * 
FROM table 
WHERE field1 is NULL

or to fetch rows where a particular field is not NULL:

SELECT * 
FROM table 
WHERE field1 is NOT NULL

In a more complex query you may want to find all rows where a certain field does not match a particular value, but also to include those rows where the field is NULL. You can do this as follows:

SELECT *
FROM table
WHERE field1<>5 OR field1 IS NULL

The point of making the test for NULL a separate test is to make sure that values that are not available or applicable do not match or mismatch values that are present. The reasoning here is that since we don't know what the value is, if it exists at all, we cannot definitively say whether it matches or doesn't.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top