MySQL基础之 逻辑运算符

mysql的逻辑运算符有四个:与、或、非、异或

我们平时在谈论的时候不考虑带有NULL的情况,今天我们就来考虑一下带有NULL值,他们的结果是怎么样的

AND(&&)运算符

mysql> select 5 AND 6,0 AND 7,0 AND NULL,3 AND NULL,9 AND 2,0 AND 12,0 AND NULL,14 AND NULL;
+---------+---------+------------+------------+---------+----------+------------+-------------+
| 5 AND 6 | 0 AND 7 | 0 AND NULL | 3 AND NULL | 9 AND 2 | 0 AND 12 | 0 AND NULL | 14 AND NULL |
+---------+---------+------------+------------+---------+----------+------------+-------------+
|       1 |       0 |          0 |       NULL |       1 |        0 |          0 |        NULL |
+---------+---------+------------+------------+---------+----------+------------+-------------+
1 row in set (0.00 sec)

 

OR(||)运算符

mysql> select 5 OR 6,0 OR 7,0 OR 0,3 OR NULL,9 || 2,0 || 12,0 || NULL,14 || NULL;
+--------+--------+--------+-----------+--------+---------+-----------+------------+
| 5 OR 6 | 0 OR 7 | 0 OR 0 | 3 OR NULL | 9 || 2 | 0 || 12 | 0 || NULL | 14 || NULL |
+--------+--------+--------+-----------+--------+---------+-----------+------------+
|      1 |      1 |      0 |         1 |      1 |       1 |      NULL |          1 |
+--------+--------+--------+-----------+--------+---------+-----------+------------+
1 row in set (0.01 sec)

 

NOT( ! )运算符

mysql> select  NOT 5,NOT 0,NOT NULL, NOT 3,NOT 0;
+-------+-------+----------+-------+-------+
| NOT 5 | NOT 0 | NOT NULL | NOT 3 | NOT 0 |
+-------+-------+----------+-------+-------+
|     0 |     1 |     NULL |     0 |     1 |
+-------+-------+----------+-------+-------+
1 row in set (0.00 sec)

 

总结:通过上面的分析我发现:NULL值是处于0和1之间的某个值,他也表示一个值,只不过这个值是NULL值,而不是0。在进行计算的时候,1与NULL则结果为NULL。而0与NULL则结果为0。1或NULL则结果为1,0或NULL则结果为NULL;可见NULL值是介于0和1之间的值。另外非NULL既不是1也不是0,还是NULL。

 

posted @ 2018-11-08 16:58  峰哥ge  阅读(2982)  评论(0编辑  收藏  举报