首页  >  文章  >  数据库  >  mysql中is null语句的用法分享

mysql中is null语句的用法分享

WBOY
WBOY原创
2016-06-07 18:03:31871浏览

对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>从主题中选择 *;
--------- ---------------- --------- -------- - ---------- ------------ ---------- ----------------- ----
|主题ID |名称 |现货 |订单 |保留 |部门 |类别 | rowupdate |
--------- ---------------- --------- --------- -- -------- ------------ ---------- -------------------- -
| 1 |爪哇 | 10 | 10 5 | 3 |热门 |摇滚| 2007-07-23 19:09:45 |
| 2 | javascript | 10 | 10 5 | 3 |经典|歌剧 | 2007-07-23 19:09:45 |
| 3 |锐利 | 17 | 17 4 | 1 |热门 |爵士乐| 2007-07-23 19:09:45 |
| 4 | c | 9 | 4 | 2 |经典|舞蹈| 2007-07-23 19:09:45 |
| 5 | c | 24 | 2 | 5 |经典|一般| 2007-07-23 19:09:45 |
| 6 |珀尔| 16 | 16 6 | 8 |经典|声乐 | 2007-07-23 19:09:45 |
| 7 |蟒蛇 | 2 | 25 | 25 6 |热门 |蓝调| 2007-07-23 19:09:45 |
| 8 | php | 32 | 32 3 | 10 | 10热门 |爵士乐| 2007-07-23 19:09:45 |
| 9 | ASP.NET | 12 | 12 15 | 15 13 |热门 |国家 | 2007-07-23 19:09:45 |
| 10 | 10 vb.net | 5 | 20 | 10 | 10热门 |新时代| 2007-07-23 19:09:45 |
| 11 | 11 vc.net | 24 | 11 | 11 14 | 14热门 |新时代| 2007-07-23 19:09:45 |
| 12 | 12统一建模语言| 42 | 42 17 | 17 17 | 17经典|一般| 2007-07-23 19:09:45 |
| 13 | www.java2s.com | 25 | 25 44 | 44 28 | 28经典|舞蹈| 2007-07-23 19:09:45 |
| 14 | 14甲骨文 | 32 | 32 15 | 15 12 | 12经典|一般| 2007-07-23 19:09:45 |
| 15 | 15 pl/sql | 20 | 10 | 10 5 |经典|歌剧 | 2007-07-23 19:09:45 |
| 16 | 16 sql服务器| 23 | 23 12 | 12 8 |经典|一般| 2007-07-23 19:09:45 |
--------- ---------------- --------- -- ------- ---------- ------------ ---------- ----------- ----------
一组 16 行(0.00 秒)

mysql>
mysql>
mysql>选择姓名、部门、​​类别
->来自主题
->其中类别为 null
->按名称排序;
空集(0.00 秒)

mysql>
mysql>
mysql>
mysql>选择姓名、部门、​​类别
->来自主题
->其中类别 = null
->按名称排序;
空集(0.00 秒)

mysql>
mysql>
mysql> drop table topic;
查询正常,0行受影响(0.00秒)

null: null不等空
null 表示“没有值”或www.3ppt.com“未知值”,且它被忽略的差异值。为了测试null,你不能使用算术比较操作符例如=、mysql>
mysql>选择姓名、部门、​​类别
->来自主题
->其中类别null
->按名称排序;
空集(0.00 秒)

mysql>
mysql> drop table topic;
查询正常,0行受影响(0.02秒)

不为空


mysql>选择姓名、部门、​​类别
->来自主题
->其中类别不为空
->按名称排序;
---------------- ------------ ----------
|名称 |部门 |类别 |
---------------- ------------ ----------
| ASP.NET |热门 |国家|
| c |经典|跳舞|
|锐利 |热门 |爵士乐|
| c |经典|一般|
|爪哇 |热门 |摇滚|
| javascript |经典|歌剧 |
|甲骨文 |经典|一般|
|珀尔 |经典|声音|
| php |热门 |爵士乐|
| pl/sql |经典|歌剧 |
|蟒蛇 |热门 |蓝调|
| sql服务器|经典|一般|
|统一建模语言|经典|一般|
| vb.net |热门 |新时代|
| vc.net |热门 |新时代|
| www.java2s.com |经典| dance |
---------------- ------------ ----------
16 行(0.00 秒)

mysql>
mysql> drop table topic;
查询正常,0行受影响(0.00秒)

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn