关于oracle中 != 及 null 使用注意点

本文深入探讨了在SQL查询中处理null值的常见问题,特别是使用等号和不等号进行比较时的注意事项。通过具体示例展示了如何正确地使用isnull、isnotnull和NVL函数来避免查询错误。

titlestepname 

关于继续开展下半年度集团早高峰保驾工作的通知传阅

 关于继续开展下半年度集团早高峰保驾工作的通知null

关于配合国资委协同办公平台统一权限配置的相关事宜null


执行如下语句:

select t.title,t.stepname from t_todo_item t where t.loginname='ST/G00100000161' and t.stepname!='传阅'

返回结果为空;


执行如下语句,结果才是希望得到的结果(stepName!='传阅')

select t.title,t.stepname from t_todo_item t where t.loginname='ST/G00100000161' and (t.stepname!='传阅' or t.stepname is null)

 关于继续开展下半年度集团早高峰保驾工作的通知null
关于配合国资委协同办公平台统一权限配置的相关事宜 null


因为null 不能用 = 或 != 来作为判断,只能使用 is null 或者 is not null

所以当 stepname!='传阅'时,null 未符合条件,故第一句的sql 是错误的。

也可以使用 nvl 函数

select t.title,t.stepname from t_todo_item t where t.loginname='ST/G00100000161' and nvl(t.stepname,'123')!='传阅'


总之,需要注意的就是 oracle 中的 null 不能用 =及!=用来比较 ,只能使用is null 或者 is not null,

当字段中有null时,需要区别对待。

说明: 
1、等价于没有任何值、是未知数。 
2、NULL与0、空字符串、空格都不同。 
3、对空值做加、减、乘、除等运算操作,结果仍为空。 
4、NULL的处理使用NVL函数。 
5、比较时使用关键字用“is null”和“is not null”。 
6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来,count(*)中,用nvl(列名,0)处理后再查。 
7、排序时比其他数据都大(索引默认是降序排列,小→大),所以NULL值总是排在最后。 

foreach 内嵌入 <trim prefix="(" suffix=")" suffixOverrides=","> <if test="searchValue != null">searchvalue,</if> <if test="createBy != null">createby,</if> <if test="createTime != null">createtime,</if> <if test="updateBy != null">updateby,</if> <if test="updateTime != null">updatetime,</if> <if test="remark != null">remark,</if> <if test="params != null">params,</if> <if test="id != null">id,</if> <if test="serialId != null and serialId != ''">serialid,</if> <if test="purchaserOrganizationId != null and purchaserOrganizationId != ''">purchaserorganizationid,</if> <if test="organizationId != null and organizationId != ''">organizationid,</if> <if test="vehicleId != null and vehicleId != ''">vehicleid,</if> <if test="purchaserOrganizationCode != null">purchaserorganizationcode,</if> <if test="erpPlantCd != null">erpplantcd,</if> <if test="materialCode != null">materialcode,</if> <if test="vehicleSeries != null">vehicleseries,</if> <if test="batchNo != null and batchno != ''">batchno,</if> <if test="arrivalYymm != null and arrivalYymm != ''">arrivalyymm,</if> <if test="quantity != null and quantity != ''">quantity,</if> <if test="contractNo != null and contractNo != ''">contractno,</if> <if test="lotNo != null and lotNo != ''">lotno,</if> <if test="itemId != null and itemId != ''">itemid,</if> <if test="itemCode != null">itemcode,</if> <if test="partDiv != null">partdiv,</if> <if test="kdType != null">kdtype,</if> <if test="pushFlag != null">pushflag,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="searchValue != null">#{searchValue},</if> <if test="createBy != null">#{createBy},</if> <if test="createTime != null">#{createTime},</if> <if test="updateBy != null">#{updateBy},</if> <if test="updateTime != null">#{updateTime},</if> <if test="remark != null">#{remark},</if> <if test="params != null">#{params},</if> <if test="id != null">#{id},</if> <if test="serialId != null and serialId != ''">#{serialId},</if> <if test="purchaserOrganizationId != null and purchaserOrganizationId != ''">#{purchaserOrganizationId},</if> <if test="organizationId != null and organizationId != ''">#{organizationId},</if> <if test="vehicleId != null and vehicleId != ''">#{vehicleId},</if> <if test="purchaserOrganizationCode != null">#{purchaserOrganizationCode},</if> <if test="erpPlantCd != null">#{erpPlantCd},</if> <if test="materialCode != null">#{materialCode},</if> <if test="vehicleSeries != null">#{vehicleSeries},</if> <if test="batchNo != null and batchNo != ''">#{batchNo},</if> <if test="arrivalYymm != null and arrivalYymm != ''">#{arrivalYymm},</if> <if test="quantity != null and quantity != ''">#{quantity},</if> <if test="contractNo != null and contractNo != ''">#{contractNo},</if> <if test="lotNo != null and lotNo != ''">#{lotNo},</if> <if test="itemId != null and itemId != ''">#{itemId},</if> <if test="itemCode != null">#{itemCode},</if> <if test="partDiv != null">#{partDiv},</if> <if test="kdType != null">#{kdType},</if> <if test="pushFlag != null">#{pushFlag},</if> </trim>
最新发布
11-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值