简体   繁体   English

MySql <> NULL和IS NOT NULL之间的区别

[英]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 在我的数据库中有一个varchar(255)列,对于某些记录,它包含空值,当我解雇它时

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: 来自Mysql 8参考:

You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL. 您不能使用算术比较运算符(如=,<或<>)来测试NULL。 Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons. 因为与NULL进行任何算术比较的结果也是NULL,所以无法从这种比较中获得任何有意义的结果。

In mysql a string column can be empty = '' or NUL (not values assigned ) 在mysql中,字符串列可以为空=''或NUL(不是赋值)

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 如果不为null,则必须使用特殊运算符IS NOT NULL

SELECT * FORM my_table where some_column IS NOT NULL;

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

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