在Oracle中,不等号有三种:<>,!=,^= ,但是并不是可以任意替换的。
来看一个例子:
1) 查询员工姓名不为scott的记录,想都没想,直接select * from emp where ename<>'scott'。
ename不为空的倒是都查出来了,但是ename为空的没查出来。
原来这么写的返回结果是“ename不为scott,且name不为空” 的记录。而我们的目的是得到“ename不为scott的全部记录”,当然这也包括ename为空的记录,所以这么写SQL语句是有问题的。为了解决这个问题,我们可以采用以下两种方案:
select * from emp where instr(concat(ename,'xx'),'scott') = 0 ;
或者
select * from emp where nvl(ename,'xx')<>'scott' ;
再看一个:
2)
查询ename不为空的记录。用select * from emp where ename <> null,结果为空。应该用 is null替换<>。
备注:null只能通过is null或者is not null来判断,其它操作符与null操作都是false