mysql中"null"的特殊性

mysql null的特殊性:

mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME  | AGE  | SEX  |
+-----+--------+------+------+
| 1   | 换换   |   23 | 男   |
| 2   | 刘丽   |   22 | 女   |
| 5   | 张友   |   22 | 男   |
+-----+--------+------+------+
3 rows in set (0.00 sec)

插入一行"null"数据做测试:

mysql> insert into student values (4,null,10,null);
Query OK, 1 row affected (0.02 sec)
mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME  | AGE  | SEX  |
+-----+--------+------+------+
| 1   | 换换   |   23 | 男   |
| 2   | 刘丽   |   22 | 女   |
| 4   | NULL   |   10 | NULL |
| 5   | 张友   |   22 | 男   |
+-----+--------+------+------+
4 rows in set (0.00 sec)

查询表中的null列:
注意:查询“sname=null”和“sname!=null”都不行.

mysql> select * from student where sname = "刘丽";
+-----+--------+------+------+
| SNO | SNAME  | AGE  | SEX  |
+-----+--------+------+------+
| 2   | 刘丽   |   22 | 女   |
+-----+--------+------+------+
1 row in set (0.00 sec)
mysql> select * from student where sname = null;
Empty set (0.00 sec)
mysql> select * from student where sname != null;
Empty set (0.00 sec)

注释:null像一个无底洞,真假都不行,"null=null"为假.

mysql> select null=null;
+-----------+
| null=null |
+-----------+
|      NULL |
+-----------+
1 row in set (0.00 sec)

注释:"null!=null"也为假.

mysql> select null!=null;
+------------+
| null!=null |
+------------+
|       NULL |
+------------+
1 row in set (0.00 sec)

注意:"0=0"为真.

mysql> select 0=0;
+-----+
| 0=0 |
+-----+
|   1 |
+-----+
1 row in set (0.00 sec)

mysql对“null”的查询有专门的语法"is"或"is not".

mysql> select * from student where sname is null;
+-----+-------+------+------+
| SNO | SNAME | AGE  | SEX  |
+-----+-------+------+------+
| 4   | NULL  |   10 | NULL |
+-----+-------+------+------+
1 row in set (0.00 sec)

mysql> select * from student where sname is not null;
+-----+--------+------+------+
| SNO | SNAME  | AGE  | SEX  |
+-----+--------+------+------+
| 1   | 换换   |   23 | 男   |
| 2   | 刘丽   |   22 | 女   |
| 5   | 张友   |   22 | 男   |
+-----+--------+------+------+
3 rows in set (0.00 sec)


猜你喜欢

转载自blog.51cto.com/215687833/2294090