Oracle中的不等于号


今天碰到一个Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下

关于Oracle中的不等于号:

在Oracle中,

<>
!=
~=
^=

都是不等于号的意思。都可以使用。

但是奇怪是的, 我想拿出price不是180000的商品时:(price是Number类型的)

SELECT id, name  FROM product where price<> 180000;

执行这个语句时,priceis null  的记录不出来。也就是拿不到price是null的商品。必须使用:

SELECT id, name  FROM product where price<> 180000 or price is null;才行。

字符串的字段存在同样的问题。

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

======================================================================================================

   测试:select * from test where name<>'xn'。只能查出name非空的记录。去掉name<>'xn'就可以了。这种写法有问题。

       然后用了instr(name,'xn')=0 来判断,如果name非空的话,判断还是有效的。如果name为空,这个判断又出问题了。不得已只得采取instr(concat(name,'xx'),'xn') = 0来判断,因为就算name为空,当和'xx'连接后,也会不为空的。

       所以最后的sql语句为:

       select * from test where instr(concat(name,'xx'),'xn') = 0 来查询name字段不等于'xn'的记录。

       或者可以用 select * from test where nvl(name,'xx')<>'xn' 来查询name字段不等于'xn'的记录。

猜你喜欢

转载自usench.iteye.com/blog/2184157