mysql not like 不去掉空值_在oracle中用like模糊搜索时如何使其能搜到空值

oracle中查询关键字 like,当我用like执行模糊查找是,发现数据量不对,表的结构如下: -- Create table create table NEOERP( ID NUMBER ( 22 ) not null , PRODUCTNAME NVARCHAR2( 100 ) default ' ' , CT_SMP_SPECIFICATION NVARCHAR2( 100 ) default ' '

oracle中查询关键字 like,当我用like执行模糊查找是,发现数据量不对,表的结构如下:

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

--Create table

create tableNEOERP

(

IDNUMBER(22) not null,

PRODUCTNAME NVARCHAR2(100) default ' ',

CT_SMP_SPECIFICATION NVARCHAR2(100) default ' ',

TYPENAME NVARCHAR2(50),

UNIT NVARCHAR2(30) default ' ',

STORECODE NVARCHAR2(100) default ' ',

ROOMCODE NVARCHAR2(100) default ' ',

SAMPTMPLID NVARCHAR2(100),

CT_BELONG NVARCHAR2(100) default ' ',

CT_MANUFACTURE NVARCHAR2(100),

CT_EXPORTER_PLACE NVARCHAR2(100),

REMARK NVARCHAR2(200),

LOGINDATE DATEdefaultsysdate,

CT_SMP_TYPE NVARCHAR2(100),

SAMPLENAME NVARCHAR2(200),

SAMPLEDESCRIPTION NVARCHAR2(200),

STARTDATE DATE,

STATUS NVARCHAR2(2) default 'F',

REMOVE_DATE DATE,

TIMETYPE NVARCHAR2(10),

REPORTDATENUMBER(22),

TIMEVALUENUMBER(22)

)

tablespace VGSM

pctfree10initrans1maxtrans255storage

(

initial 64K

minextents1maxextents unlimited

);--Add comments to the columns

comment on columnNEOERP.PRODUCTNAMEis '产品名称';

commenton columnNEOERP.CT_SMP_SPECIFICATIONis '规格';

commenton columnNEOERP.TYPENAMEis '类别名称';

commenton columnNEOERP.UNITis '单位';

commenton columnNEOERP.STORECODEis '存货编码';

commenton columnNEOERP.ROOMCODEis '物料编码';

commenton columnNEOERP.SAMPTMPLIDis '样品模版id';

commenton columnNEOERP.CT_BELONGis '归属地';

commenton columnNEOERP.CT_MANUFACTUREis '生产地';

commenton columnNEOERP.CT_EXPORTER_PLACEis '出口地';

commenton columnNEOERP.REMARKis '备注';

commenton columnNEOERP.CT_SMP_TYPEis '样品类型';

commenton columnNEOERP.SAMPLENAMEis '样品名称';

commenton columnNEOERP.SAMPLEDESCRIPTIONis '样品描述';

commenton columnNEOERP.STARTDATEis '起始时间';

commenton columnNEOERP.STATUSis 'f可用 状态';

commenton columnNEOERP.REMOVE_DATEis '删除时间';

commenton columnNEOERP.TIMETYPEis '年月日';

commenton columnNEOERP.REPORTDATEis '要求报告日期';

commenton columnNEOERP.TIMEVALUEis '有效期';--Create/Recreate primary, unique and foreign key constraints

alter tableNEOERPadd constraint PK_ERP_ID primary key(ID)

usingindextablespace VGSM

pctfree10initrans2maxtrans255storage

(

initial 64K

minextents1maxextents unlimited

);

test.jsp?url=http%3A%2F%2Fcommon.cnblogs.com%2Fimages%2Fcopycode.gif&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

当我用SELECT * FROM NEOERP WHERE ROOMCODE LIKE '%%',来查询时候,数据一直存在问题,查处的数据量与期望的数据量不同,

经查,是当roomcode为null造成的,

当我用SELECT * FROM NEOERP WHERE ROOMCODE IS NULL 来查询发现这个查询的结果是我所缺少的数据信息。

null的含义,在我们不知道具体有什么数据,也即未知,称他为空,oracle中,含有空值的表列长度为零。

等价于没有任何值,是未知数,null与0,空字符串,空格不同,对空值做运算,结果仍然是空值,oracle提供了处理空值函数nvl,比较时候用 is null或者 is not null.

通过以上说明,通过SELECT * FROM NEOERP WHERE ROOMCODE LIKE '%%'查询不到值为null的数据。

对上面sql语句进行修改后,既可以完成所需功能:

SELECT * FROM NEOERP WHERE NVL(ROOMCODE,0) LIKE '%%';

注释:

当ROOMCODE为null时,NVL(ROOMCODE,0)的返回值为0,而0正好符合匹配LIKE

'%%'这个匹配模式,所以,ROOMCODE为null的数据行可以被查询出来。

附加:

空字符串"可以被like`%%`搜索到。

参考:

关于null的说明以及和0的区别

百度 like ‘%%’空值

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

SQL模糊查询碰到空值怎么办?

作者:iamlaosong

SQL查询语句用%来做模糊查询,程序中一般要求用户输入部分信息,根据这个信息进行模糊查询。例如用户输入340104,下面这条语句就是查询昨天客户代码为340104开头的所有邮件信息:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andt.sender_cust_codelike'340104%'

当用户什么都不输入需要查询昨天所有邮件信息时,下面的语句并不能查询到所有信息,这条语句只能查到所有大客户的邮件信息,查不到散户的邮件信息:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andt.sender_cust_codelike'%'

这是因为散户的客户代码为空值,下面这条语句可以同时兼顾上面两种情形(假定用0000代表空值):

有限定值:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andnvl(t.sender_cust_code,'0000')like'340104%'

无限定值:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andnvl(t.sender_cust_code,'0000')like'%'

限定值是0000时结果是所有散户:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andnvl(t.sender_cust_code,'0000')like'0000%'

除了like,not like、not in 、<>等运算符号也都不包含空值,例如下面语句并不包含客户代码为空值的记录:

[sql]

view plaincopy

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2FCODE_ico.png&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

test.jsp?url=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125%2Fhttps%3A%2Fcode.csdn.net%2Fassets%2Fico_fork.svg&refer=http%3A%2F%2Fblog.csdn.net%2Fhaiross%2Farticle%2Fdetails%2F43272125

select*fromtb_evt_mail_clct t

wheret.clct_date = trunc(sysdate - 1)

andt.sender_cust_code <>'34122600200300'

要想包含,同样需要将条件改为:nvl(t.sender_cust_code,'0000') <> '34122600200300'

总之,当一个字段为空值时,表达式中无论是等于还是不等于,结果都为假,只有 is null结果为真。

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值