当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL NULL 值处理实例详解

MySQL NULL 值处理实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

mysql null 值处理

我们已经知道mysql使用 sql select 命令及 where 子句来读取数据表中的数据,但是当提供的查询条件字段为 null 时,该命令可能就无法正常工作。

为了处理这种情况,mysql提供了三大运算符:

  • is null: 当列的值是null,此运算符返回true。
  • is not null: 当列的值不为null, 运算符返回true。
  • <=>: 比较操作符(不同于=运算符),当比较的的两个值为null时返回true。

关于 null 的条件比较运算是比较特殊的。你不能使用 = null 或 != null 在列中查找 null 值 。

在mysql中,null值与任何其它值的比较(即使是null)永远返回false,即 null = null 返回false 。

mysql中处理null使用is null和is not null运算符。

在命令提示符中使用 null 值

以下实例中假设数据库 tutorials 中的表 tcount_tbl 含有两列 tutorial_author 和 tutorial_count, tutorial_count 中设置插入null值。

实例

尝试以下实例:

root@host# mysql -u root -p password;
enter password:*******
mysql> use tutorials;
database changed
mysql> create table tcount_tbl
  -> (
  -> tutorial_author varchar(40) not null,
  -> tutorial_count int
  -> );
query ok, 0 rows affected (0.05 sec)
mysql> insert into tcount_tbl
  -> (tutorial_author, tutorial_count) values ('mahran', 20);
mysql> insert into tcount_tbl
  -> (tutorial_author, tutorial_count) values ('mahnaz', null);
mysql> insert into tcount_tbl
  -> (tutorial_author, tutorial_count) values ('jen', null);
mysql> insert into tcount_tbl
  -> (tutorial_author, tutorial_count) values ('gill', 20);

mysql> select * from tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran     |       20 |
| mahnaz     |      null |
| jen       |      null |
| gill      |       20 |
+-----------------+----------------+
4 rows in set (0.00 sec)

mysql>

以下实例中你可以看到 = 和 != 运算符是不起作用的:

mysql> select * from tcount_tbl where tutorial_count = null;
empty set (0.00 sec)
mysql> select * from tcount_tbl where tutorial_count != null;
empty set (0.01 sec)

查找数据表中 tutorial_count 列是否为 null,必须使用is null和is not 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 值

php脚本中你可以在 if...else 语句来处理变量是否为空,并生成相应的条件语句。

以下实例中php设置了$tutorial_count变量,然后使用该变量与数据表中的 tutorial_count 字段进行比较:

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die('could not connect: ' . mysql_error());
}
if( isset($tutorial_count ))
{
  $sql = 'select tutorial_author, tutorial_count
      from tcount_tbl
      where tutorial_count = $tutorial_count';
}
else
{
  $sql = 'select tutorial_author, tutorial_count
      from tcount_tbl
      where tutorial_count is $tutorial_count';
}

mysql_select_db('tutorials');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die('could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, mysql_assoc))
{
  echo "author:{$row['tutorial_author']} <br> ".
     "count: {$row['tutorial_count']} <br> ".
     "--------------------------------<br>";
} 
echo "fetched data successfully\n";
mysql_close($conn);
?>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网