mysql中is null语句的用法分享_Mysql

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)

时间: 2024-09-02 20:56:50

mysql中is null语句的用法分享_Mysql的相关文章

小心陷阱!MySQL中处理Null时需注意两点_Mysql

MySQL数据库是一个基于结构化数据的开源数据库.SQL语句是MySQL数据库中核心语言.不过在MySQL数据库中执行SQL语句,需要小心两个陷阱. 陷阱一:空值不一定为空 空值是一个比较特殊的字段.在MySQL数据库中,在不同的情形下,空值往往代表不同的含义.这是MySQL数据库的一种特性.如在普通的字段中(字符型的数据),空值就是表示空值.但是如果将一个空值的数据插入到TimesTamp类型的字段中,空值就不一定为空.此时为出现什么情况呢(如下图)? 我先创建了一个表.在这个表中有两个字段:

mysql中order by 语句的用法 索引优化

MySQL Order By keyword是用来给记录中的数据进行分类的. MySQL Order By Keyword根据关键词分类 ORDER BY keyword是用来给记录中的数据进行分类的. MySQL Order By语法  代码如下 复制代码 SELECT column_name(s) FROM table_name ORDER BY column_name 注意:SQL语句是"字母大小写不敏感"的语句(它不区分字母的大小写),即:"ORDER BY"

MySQL中replace into语句的用法详解_Mysql

在向表中插入数据的时候,经常遇到这样的情况: 1.首先判断数据是否存在: 2.如果不存在,则插入: 3.如果存在,则更新.   在 SQL Server 中可以这样写: 复制代码 代码如下: if not exists (select 1 from table where id = 1) insert into table(id, update_time) values(1, getdate()) else update table set update_time = getdate() whe

MySQL中安装样本数据库Sakila过程分享_Mysql

通常情况下对于一个全新的MySQL服务器,没有任何数据供我们测试和使用.对此,MySQL为我们提供了一些样本数据库,我们可以基于这些数据库作基本的操作以及压力测试等等.本文描述的是安装sakila数据库.该数据库需要安装在MySQL 5.0以上的版本.以下是其描述. 1.下载种子数据库 下载位置:http://dev.mysql.com/doc/index-other.html 2.安装种子数据库sakila 复制代码 代码如下: [root@localhost ~]# unzip sakila

MySQL中使用SQL语句查看某个表的编码方法_Mysql

MySQL中,如何使用SQL语句来查看某个表的编码呢?我们使用show create table 这一SQL语句来解决这个问题. show create table可以查看创建这个表的SQL语句脚本,它的基本语法是: show create table <表名>; 我们用它看看test表的create脚本: mysql> show create table test; +-------+--------------------------------------------- -----

MySQL中优化sql语句查询常用的30种方法

本篇文章是对MySQL中优化sql语句查询常用的30种方法进行了详细的分析介绍,需要的朋友参考下   1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以

MySQL中create table语句的基本语法是_php基础

MySQL中create table语句的基本语法是:  Create [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]  [table_options] [select_statement]  TEMPORARY:该关键字表示用create table新建的表为临时表,此表在当前会话结束后将自动消失.临时表主要被应用于存储过程中,对于目前尚不支持存储过程的MySQL,该关键字一般不用.  IF NOT EX

mysql中case 和if的用法

问题描述 mysql中case 和if的用法 这是我想要写的,但是case when里面是不是只能写一个条件?我想要两个条件去判断,返回一列.用if怎么写呢? 解决方案 不是,你这样写就是可以的. 解决方案二: 可以用when and,最好用括号括起来,然后就是检查你的判断条件是否正确 SELECT table1.id, table1.name, CASE WHEN (table1.event = 'r' AND table1.name='Jones') THEN 'very high' WHE

MySQL中使用SQL语句对字段进行重命名_Mysql

MySQL中,如何使用SQL语句来对表中某一个字段进行重命名呢?我们将使用alter table 这一SQL语句. 重命名字段的语法为:alter table <表名> change <字段名> <字段新名称> <字段的类型>. 现在我们来尝试把test表中的t_name字段重命名为t_name_new字段. 1.首先查看一下当前test表的结构 mysql> describe test; +------------+-------------+---