MySql中的“<> NULL”和“IS NOT NULL”的区别?
如果您将“<>”运算符与NULL值进行比较,则始终会得到NULL值,而没有结果。
让我们看一些比较的例子−
mysql> select 10 <> NULL;
+------------+
| 10 <> NULL |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> select NULL <> NULL;
+--------------+
| NULL <> NULL |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
mysql> select 'Chris' <> NULL;
+-----------------+
| 'Chris' <> NULL |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)
mysql> select '' <> NULL;
+------------+
| '' <> NULL |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
首先让我们创建一个表−
mysql> create table DemoTable846(Value int);
Query OK, 0 rows affected (0.93 sec)
使用insert命令向表中插入一些记录−
mysql> insert into DemoTable846 values(NULL);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable846 values(10);
Query OK, 1 row affected (0.11 sec)
使用select语句显示表中的所有记录−
mysql> select *from DemoTable846;
这会产生以下输出−
+-------+
| Value |
+-------+
| NULL |
| 10 |
+-------+
2 rows in set (0.00 sec)
IS NOT NULL运算符的结果如下−
mysql> select *from DemoTable846 where Value IS NOT NULL;
这会产生以下输出−
+-------+
| Value |
+-------+
| 10 |
+-------+
1 row in set (0.00 sec)
下面是“<>”运算符的结果。以下是查询−
mysql> select *from DemoTable846 where Value <> NULL;
Empty set (0.00 sec)
阅读更多:MySQL 教程