MySQL NULL 值处理实例教程

零下一度
发布: 2017-05-15 10:31:24
原创
1657人浏览过

MySQL NULL值处理

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

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

IS NULL:当列的值为NULL,此运算符返回true。

IS NOT NULL:当列的值不为NULL,运算符返回true。

<=>:  比较操作符(不同于=运算符),当比较的的两个值为NULL时返回真。

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

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

MySQL中处理NULL使用IS NULL和IS NOT NULL运算符。

在命令提示符中使用NULL值

以下实例中假设数据库指南中的表tcount_tbl包含两列tutorial_author和tutorial_count,tutorial_count中设置插入NULL值。

尝试以下实例:

ViiTor实时翻译
ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116
查看详情 ViiTor实时翻译
root @ host#mysql -u root -p password;
输入密码:*******
mysql> use TUTORIALS;数据库已更改mysql> create table tcount_tbl
    - >(
    - > tutorial_author varchar(40)NOT NULL,
    - > tutorial_count INT
    - >);
查询OK,0行受影响(0.05秒)
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值('mahran',20);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)values('mahnaz',NULL);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值('Jen',NULL);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值('Gill',20);
mysql> select * from tcount_tbl;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| mahnaz | NULL |
| 仁| NULL |
| 鳃| 20 |
+ ----------------- + ---------------- +
4行(0.00秒)
MySQL的>
登录后复制

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

mysql> SELECT * FROM tcount_tbl WHERE tutorial_count = NULL;
空置(0.00秒)
mysql> SELECT * FROM tcount_tbl WHERE tutorial_count!= NULL;
空置(0.01秒)
登录后复制

查询数据表中tutorial_count列是否为NULL,必须使用IS NULL和IS NOT NULL,如下实例:

mysql> SELECT * FROM tcount_tbl 
    - > WHERE tutorial_count IS NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| mahnaz | NULL |
| 仁| NULL |
+ ----------------- + ---------------- +
2行(0.00秒)
mysql> select * from tcount_tbl 
    - > WHERE tutorial_count is NOT NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| 鳃| 20 |
+ ----------------- + ---------------- +
2行(0.00秒)
登录后复制

使用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('无法连接:'。mysql_error());
}
if(isset($ tutorial_count))
{
   $ sql ='SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count = $ tutorial_count';
}
其他
{
   $ sql ='SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count IS $ tutorial_count';
}
mysql_select_db( '教程');
$ retval = mysql_query($ sql,$ conn);
如果(!$ retval)
{
  die('无法获取数据:'mysql_error());
}
while($ row = mysql_fetch_array($ retval,MYSQL_ASSOC))
{
    echo“作者:{$ row ['tutorial_author']} <br>”。
         “Count:{$ row ['tutorial_count']} <br>”。
         “--------------------------------结果”;
} 
echo“成功获取数据\ n”;
mysql_close($康恩);
?>
登录后复制

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本下载

2. 免费mysql在线视频教程

3. 数据库设计那些事

以上就是MySQL NULL 值处理实例教程的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号