Rumah  >  Artikel  >  pangkalan data  >  MySQL NULL 值处理实例教程

MySQL NULL 值处理实例教程

零下一度
零下一度asal
2017-05-15 10:31:241315semak imbas

MySQL NULL值处理

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

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

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

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

96b4fef55684b9312718d5de63fb7121:  比较操作符(不同于=运算符),当比较的的两个值为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值。

尝试以下实例:

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 =&#39;localhost:3036&#39;;
$ dbuser =&#39;root&#39;;
$ dbpass =&#39;rootpassword&#39;;
$ conn = mysql_connect($ dbhost,$ dbuser,$ dbpass);
if(!$ conn)
{
  die(&#39;无法连接:&#39;。mysql_error());
}
if(isset($ tutorial_count))
{
   $ sql =&#39;SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count = $ tutorial_count&#39;;
}
其他
{
   $ sql =&#39;SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count IS $ tutorial_count&#39;;
}
mysql_select_db( &#39;教程&#39;);
$ retval = mysql_query($ sql,$ conn);
如果(!$ retval)
{
  die(&#39;无法获取数据:&#39;mysql_error());
}
while($ row = mysql_fetch_array($ retval,MYSQL_ASSOC))
{
    echo“作者:{$ row [&#39;tutorial_author&#39;]} <br>”。
         “Count:{$ row [&#39;tutorial_count&#39;]} <br>”。
         “--------------------------------结果”;
} 
echo“成功获取数据\ n”;
mysql_close($康恩);
?>

【相关推荐】

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

2. 免费mysql在线视频教程

3. 数据库设计那些事

Atas ialah kandungan terperinci MySQL NULL 值处理实例教程. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Mysql Join的使用教程Artikel seterusnya:MySQL 事务实例教程