加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MYSQL教程MySQL NULL 值处理实例详解

发布时间:2020-12-12 01:00:26 所属栏目:MySql教程 来源:网络整理
导读:《MYSQL教程MySQL NULL 值处理实例详解》要点: 本文介绍了MYSQL教程MySQL NULL 值处理实例详解,希望对您有用。如果有疑问,可以联系我们。 MySQL NULL 值处理 MYSQL必读 我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提

《MYSQL教程MySQL NULL 值处理实例详解》要点:
本文介绍了MYSQL教程MySQL NULL 值处理实例详解,希望对您有用。如果有疑问,可以联系我们。

MySQL NULL 值处理MYSQL必读

我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作.MYSQL必读

为了处理这种情况,MySQL提供了三大运算符:MYSQL必读

  • IS NULL: 当列的值是NULL,此运算符返回true.
  • IS NOT NULL: 当列的值不为NULL,运算符返回true.
  • <=>: 比较操作符(不同于=运算符),当比较的的两个值为NULL时返回true.

关于 NULL 的条件比较运算是比较特殊的.你不能使用 = NULL 或 != NULL 在列中查找 NULL 值 .MYSQL必读

在MySQL中,NULL值与任何其它值的比较(即使是NULL)永远返回false,即 NULL = NULL 返回false .MYSQL必读

MySQL中处理NULL使用IS NULL和IS NOT NULL运算符.MYSQL必读

在命令提示符中使用 NULL 值MYSQL必读

以下实例中假设数据库 TUTORIALS 中的表 tcount_tbl 含有两列 tutorial_author 和 tutorial_count,tutorial_count 中设置插入NULL值.MYSQL必读

实例MYSQL必读

尝试以下实例:MYSQL必读

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',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必读

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必读

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 值MYSQL必读

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

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

<?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 successfullyn";
mysql_close($conn);
?>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读