简体   繁体   中英

Difference Between MySql <> NULL and IS NOT NULL

In my db there is a varchar(255) column and for some records it contains null values, when i fired this

SELECT * FORM my_table where some_column <> NULL;

nothing is returned

but when is fired

SELECT * FORM my_table where some_column IS NOT NULL;

I got desired records

can you explain what's the main difference between them and when to use <> and != operators.

From the Mysql 8 Reference:

You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL. Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.

In mysql a string column can be empty = '' or NUL (not values assigned )

so you should use for not empty

  SELECT * FORM my_table where some_column <> '';

or

  SELECT * FORM my_table where some_column  != '';

or not equal to a value

  SELECT * FORM my_table where some_column <> 'my_value';
  SELECT * FORM my_table where some_column != 'my_value';

for not null you must use the special operator IS NOT NULL

SELECT * FORM my_table where some_column IS NOT 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