欢迎投稿

今日深度:

Oracle中不等于号问题

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);

如图:

\

字段NAME和AGE都有空值

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

select * from test where  age <> 23;

\

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

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

\

以上两个例子严格意义上说均不符合我们的要求,因为没有把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;



www.htsjk.Com true http://www.htsjk.com/oracle/23363.html NewsArticle 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) v...
评论暂时关闭