• SQL之NULL值的几种处理方式


    1、创建测试表:

    drop table if exists tab_null_operator;
    create table tab_null_operator as
    select 1 as id,'chavin' as name union all
    select 2 as id,'nope' as name union all
    select 3 as id,'' as name union all
    select 4 as id,'' as name union all
    select 5 as id,null as name union all
    select 6 as id,null as name union all
    select 7 as id,' ' as name union all
    select 8 as id,' ' as name union all
    select 9 as id,'    ' as name union all
    select 10 as id,'    ' as name
    ;

    2、查看数据:

    mysql> select * from tab_null_operator;
    +----+--------+
    | id | name   |
    +----+--------+
    |  1 | chavin |
    |  2 | nope   |
    |  3 |        |
    |  4 |        |
    |  5 | NULL   |
    |  6 | NULL   |
    |  7 |        |
    |  8 |        |
    |  9 |        |
    | 10 |        |
    +----+--------+
    10 rows in set (0.00 sec)

    小结:从结果我们可以看出,id in (5,6)的两个值是null,id in (1,2)的是字符串,id in (3,4,7,8,9,10)从结果看均是空字符串,实际id in (3,4)是空字符串,id in (7,8)是单个空格字符串,id in (9,10)是 字符串。

    3、查询name为null的记录:

    mysql> select * from tab_null_operator where name is null;
    +----+------+
    | id | name |
    +----+------+
    |  5 | NULL |
    |  6 | NULL |
    +----+------+
    2 rows in set (0.00 sec)

    小结:可以看到只有id in (5,6)的记录name字段才是真正的null。

    4、查询name为''的记录信息:

    mysql> select * from tab_null_operator where name = '';
    +----+------+
    | id | name |
    +----+------+
    |  3 |      |
    |  4 |      |
    |  7 |      |
    |  8 |      |
    +----+------+
    4 rows in set (0.00 sec)

    小结:可以看到我们输入的以空格为字符串的值都表现为空字符串。然后 字符串的缺没有筛选出来。

    5、查询 字符串的数据:

    mysql> select * from tab_null_operator where name = '   ';
    +----+------+
    | id | name |
    +----+------+
    |  9 |      |
    | 10 |      |
    +----+------+
    2 rows in set (0.00 sec)

  • 相关阅读:
    RabbitMQ
    虚拟化解决方案
    如何制作Windows镜像
    2018-2019-2 网络对抗技术 20165202 Exp9 Web安全基础
    2018-2019-2 网络对抗技术 20165202 Exp8 Web基础
    2018-2019-2 网络对抗技术 20165202 Exp7 网络欺诈防范
    2018-2019-2 网络对抗技术 20165202 Exp6 信息搜集与漏洞扫描
    2018-2019-2 网络对抗技术 20165202 Exp5 MSF基础应用
    2018-2019-2 网络对抗技术 20165202 Exp4 恶意代码分析
    2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
  • 原文地址:https://www.cnblogs.com/wcwen1990/p/10039317.html
Copyright © 2020-2023  润新知