我们已经看到sql select命令和where子句一起使用,来从mysql表中提取数据,但是,当我们试图给出一个条件,比较字段或列值设置为null,它确不能正常工作。
为了处理这种情况,mysql提供了三大运算符
-
is null: 如果列的值为null,运算结果返回 true
-
is not null: 如果列的值不为null,运算结果返回 true
-
<=>: 运算符比较值,(不同于=运算符)即使两个空值它返回 true
涉及null的条件是特殊的。不能使用= null或!= null来匹配查找列的null值。这样的比较总是失败,因为它是不可能告诉它们是否是true。 甚至 null = null 也是失败的。
要查找列的值是或不是null,使用is null或is not null。
在命令提示符,使用null值
假设在 test 数据库中的表 tcount_tbl 它包含两个列 tutorial_author 和 tutorial_count, 其中 tutorial_count 的值为null表明其值未知:
示例
试试下面的例子:
mysql> select * from tcount_tbl
-> where tutorial_count is null; +-----------------+----------------+ | tutorial_author | tutorial_count | +-----------------+----------------+ | mahnaz | null | | jen | null | +-----------------+----------------+ 2 rows in set (0.00 sec) mysql> select * from tcount_tbl
-> where tutorial_count is not null; +-----------------+----------------+ | tutorial_author | tutorial_count | +-----------------+----------------+ | mahran | 20 | | gill | 20 | +-----------------+----------------+ 2 rows in set (0.00 sec) 在php脚本中处理null值
可以使用 if...else 条件来基于null值的查询。
示例
下面的示例,从外面使用 tutorial_count,然后在表中可用的值进行比较。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!