SQL Where Not Equal Does Not Return NULL Results

table 'materials'
id   sku  content
10   IT2   Iron
11   IT3   Steel
12   IT4   Steel
13   IT5   NULL
14   IT6   Iron
15   IT7   Glass

select id, sku, content from materials where content !='Iron';

Return Result:

id   sku   content
11   IT3   Steel
12   IT4   Steel
15   IT7   Glass

Why is id # 13 with a NULL value not returned in the result set? Using MYSQL.

+4
source share
1 answer

According to your suggestion, whereit compares null != 'Iron', which calculates the UNKNOWN value, which is neither true nor false, based on SQL 3 logic. Therefore, the request is not returned.

If you need a string nullto be cast, you should use

where content !='Iron' or content is null
+6
source

Source: https://habr.com/ru/post/1605998/


All Articles