mysql中is null语句的用法分享

 更新时间:2011年05月07日 22:00:30   作者:  
对null的特殊处理即是在前面的章节中,为了决定哪个动物不再是活着的,使用death is not null而不使用death != null的原 因

mysql数据库中is null语句的用法

注意在mysql中,0或 null意味着假而其它值意味着真。布尔运算的默认真值是1。

对null的特殊处理即是在前面的章节中,为了决定哪个动物不再是活着的,使用death is not null而不使用death != null的原因。

在group by中,两个null值视为相同。

执行order by时,如果运行 order by ... asc,则null值出现在最前面,若运行order by ... desc,则null值出现在最后面。

null操作的常见错误是不能在定义为not null的列内插入0或空字符串,但事实并非如此。在null表示"没有数值"的地方有数值

。使用is [not] null则可以很容易地进行测试


is null or = null


mysql>
mysql> create table topic(
    ->    topicid     smallint not null auto_increment primary key,
    ->    name        varchar(50) not null,
    ->    instock     smallint unsigned not null,
    ->    onorder     smallint unsigned not null,
    ->    reserved    smallint unsigned not null,
    ->    department  enum('classical', 'popular') not null,
    ->    category    varchar(20) not null,
    ->    rowupdate   timestamp not null
    -> );
query ok, 0 rows affected (0.02 sec)

mysql>
mysql>
mysql> insert into topic (name,          instock, onorder, reserved, department,   category) values
    ->                   ('java',          10,      5,       3,        'popular',    'rock'),
    ->                   ('css',    10,      5,       3,        'classical',  'opera'),
    ->                   ('c sharp',       17,      4,       1,        'popular',    'jazz'),
    ->                   ('c',             9,       4,       2,        'classical',  'dance'),
    ->                   ('c++',           24,      2,       5,        'classical',  'general'),
    ->                   ('perl',          16,      6,       8,        'classical',  'vocal'),
    ->                   ('python',        2,       25,      6,        'popular',    'blues'),
    ->                   ('php',           32,      3,       10,       'popular',    'jazz'),
    ->                   ('asp.net',       12,      15,      13,       'popular',    'country'),
    ->                   ('vb.net',        5,       20,      10,       'popular',    'new age'),
    ->                   ('vc.net',        24,      11,      14,       'popular',    'new age'),
    ->                   ('uml',           42,      17,      17,       'classical',  'general'),
    ->                   ('www.java2s.com',25,      44,      28,       'classical',  'dance'),
    ->                   ('oracle',        32,      15,      12,       'classical',  'general'),
    ->                   ('pl/sql',        20,      10,      5,        'classical',  'opera'),
    ->                   ('sql server',    23,      12,      8,        'classical',  'general');
query ok, 16 rows affected (0.00 sec)
records: 16  duplicates: 0  warnings: 0

mysql>
mysql> select * from topic;
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
| topicid | name           | instock | onorder | reserved | department | category | rowupdate           |
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
|       1 | java           |      10 |       5 |        3 | popular    | rock     | 2007-07-23 19:09:45 |
|       2 | javascript     |      10 |       5 |        3 | classical  | opera    | 2007-07-23 19:09:45 |
|       3 | c sharp        |      17 |       4 |        1 | popular    | jazz     | 2007-07-23 19:09:45 |
|       4 | c              |       9 |       4 |        2 | classical  | dance    | 2007-07-23 19:09:45 |
|       5 | c++            |      24 |       2 |        5 | classical  | general  | 2007-07-23 19:09:45 |
|       6 | perl           |      16 |       6 |        8 | classical  | vocal    | 2007-07-23 19:09:45 |
|       7 | python         |       2 |      25 |        6 | popular    | blues    | 2007-07-23 19:09:45 |
|       8 | php            |      32 |       3 |       10 | popular    | jazz     | 2007-07-23 19:09:45 |
|       9 | asp.net        |      12 |      15 |       13 | popular    | country  | 2007-07-23 19:09:45 |
|      10 | vb.net         |       5 |      20 |       10 | popular    | new age  | 2007-07-23 19:09:45 |
|      11 | vc.net         |      24 |      11 |       14 | popular    | new age  | 2007-07-23 19:09:45 |
|      12 | uml            |      42 |      17 |       17 | classical  | general  | 2007-07-23 19:09:45 |
|      13 | www.java2s.com |      25 |      44 |       28 | classical  | dance    | 2007-07-23 19:09:45 |
|      14 | oracle         |      32 |      15 |       12 | classical  | general  | 2007-07-23 19:09:45 |
|      15 | pl/sql         |      20 |      10 |        5 | classical  | opera    | 2007-07-23 19:09:45 |
|      16 | sql server     |      23 |      12 |        8 | classical  | general  | 2007-07-23 19:09:45 |
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
16 rows in set (0.00 sec)

mysql>
mysql>
mysql> select name, department, category
    -> from topic
    -> where category is null
    -> order by name;
empty set (0.00 sec)

mysql>
mysql>
mysql>
mysql> select name, department, category
    -> from topic
    -> where category = null
    -> order by name;
empty set (0.00 sec)

mysql>
mysql>
mysql> drop table topic;
query ok, 0 rows affected (0.00 sec)

 <=>null: null不等空
null意味着“没有值”或www.3ppt.com“未知值”,且它被看作与众不同的值。为了测试null,你不能使用算术比较 操作符例如=、<或!=
mysql>
mysql> select name, department, category
    -> from topic
    -> where category<=>null
    -> order by name;
empty set (0.00 sec)

mysql>
mysql> drop table topic;
query ok, 0 rows affected (0.02 sec)

is not null


mysql> select name, department, category
    -> from topic
    -> where category is not null
    -> order by name;
+----------------+------------+----------+
| name           | department | category |
+----------------+------------+----------+
| asp.net        | popular    | country  |
| c              | classical  | dance    |
| c sharp        | popular    | jazz     |
| c++            | classical  | general  |
| java           | popular    | rock     |
| javascript     | classical  | opera    |
| oracle         | classical  | general  |
| perl           | classical  | vocal    |
| php            | popular    | jazz     |
| pl/sql         | classical  | opera    |
| python         | popular    | blues    |
| sql server     | classical  | general  |
| uml            | classical  | general  |
| vb.net         | popular    | new age  |
| vc.net         | popular    | new age  |
| www.java2s.com | classical  | dance    |
+----------------+------------+----------+
16 rows in set (0.00 sec)

mysql>
mysql> drop table topic;
query ok, 0 rows affected (0.00 sec)

相关文章

  • MySQL数据库字符集修改中文UTF8(永久修改)

    MySQL数据库字符集修改中文UTF8(永久修改)

    本文主要介绍了MySQL数据库字符集修改中文UTF8,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • MySQL字符集utf8修改为utf8mb4的方法步骤

    MySQL字符集utf8修改为utf8mb4的方法步骤

    这篇文章主要给大家介绍了关于MySQL字符集utf8修改为utf8mb4的方法步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用MySQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • Navicat For MySQL的简单使用教程

    Navicat For MySQL的简单使用教程

    这篇文章主要介绍了Navicat For MySQL的简单使用教程,本文给大家介绍的非常详细,具有一定的参考借鉴价,需要的朋友可以参考下
    2019-06-06
  • mysql中数据统计的技巧备忘录

    mysql中数据统计的技巧备忘录

    mysql是常用数据库,对于数字操作相关的东西相当方便,这篇文章主要给大家介绍了关于mysql中数据统计技巧的相关资料,非常具有实用价值,需要的朋友可以参考下
    2018-05-05
  • insert into … on duplicate key update / replace into 多行数据介绍

    insert into … on duplicate key update / replace into 多行数据介绍

    当我插入一条数据时,我要判断(k1,k2)是否已经存在(1条selete),若存在就update,不存在就insert
    2013-08-08
  • mysql 字段定义不要用null的原因分析

    mysql 字段定义不要用null的原因分析

    这篇文章主要介绍了mysql 字段定义不要用null的原因分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • 高效的mysql分页方法及原理

    高效的mysql分页方法及原理

    这篇文章介绍了高效的mysql分页方法及原理,有需要的朋友可以参考一下
    2013-08-08
  • MySQL动态SQL拼接实例详解

    MySQL动态SQL拼接实例详解

    动态SQL呢?首先是SQL语句,是根据条件来拼接SQL,下面这篇文章主要给大家介绍了关于MySQL动态SQL拼接的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • MySql学习笔记之事务隔离级别详解

    MySql学习笔记之事务隔离级别详解

    这篇文章主要给大家介绍了关于MySql学习笔记之事务隔离级别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • 使用mysql中遇到的几个问题

    使用mysql中遇到的几个问题

    首先mysql不是可视化的,可以通过命令行进行操作,包括创建数据库、表、添加数据等等。那岂不是很不方便了吗?
    2009-07-07

最新评论