当前位置:首页 > 数据库 > Oracle

Oracle中不等于号问题

 

测试SQL

create table test(
  id int,
  name varchar2(10),
  age int
)

insert into test(id,name,age) values(1,‘zhangsan‘,23);
insert into test(id,name,age) values(2,‘lisi‘,‘‘);
insert into test(id,name,age) values(3,‘wangwu‘,null);
insert into test(id,name,age) values(4,‘sunqi‘,27);
insert into test(id,name,age) values(5,‘‘,22);

如图:

Oracle中不等于号问题 - 文章图片

字段NAME和AGE都有空值

 

例1、查询AGE不等于23的数据

select * from test where  age <> 23;

Oracle中不等于号问题 - 文章图片

 

例2、查询NAME不为lisi的数据

select * from test where name != ‘lisi‘;

Oracle中不等于号问题 - 文章图片

 

以上两个例子严格意义上说均不符合我们的要求,因为没有把null值查询出来

null只能通过is null或者is not null来判断,其它操作符与null操作都是false。

 

最后正确的sql语句为:

select * from test where instr(concat(name,‘xx‘),‘lisi‘) = 0; --查询name字段不等于‘lisi‘的记录
或
select * from test where nvl(name,‘xx‘)<>‘lisi‘; 
select * from test where instr(concat(age,00),23) = 0; --查询age字段不等于23的记录
或
select * from test where nvl(age,00)<>23;


 作者:itmyhome


Oracle中不等于号问题

标签:oracle   不等于号   

本文系统来源:http://blog.csdn.net/itmyhome1990/article/details/45918363


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐