문제

I get different results when i run select queries with != NULL OR IS NOT NULL .

    /** No results returned.*/
        SELECT *
        FROM PORT_INFO
        WHERE PORT_CODE != NULL;

   /** Results are returned.*/
    SELECT *
    FROM PORT_INFO
    WHERE PORT_CODE IS NOT NULL;
  • In My Table, PORT_CODE column is nullable.
  • Column is of type VARCHAR(4) .
  • MySql Version 5.5.13 enterprise.
도움이 되었습니까?

해결책

From the documentation:

Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.

So basically IS NOT NULL will actually do what you intend, while != will do nothing useful (since it too will just return NULL).

다른 팁

In SQL, there is no value that is equal to NULL, including NULL itself. To get what you expect, use IS NOT NULL.

Any comparison or operation with NULL returns NULL, with the exception of IS NULL.

A NULL boolean is considered to be false.

So, expressions like <> NULL, = NULL, and so on are always false.

This is ANSI standard behavior.

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