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执行模糊查找是,发现数据量不对,表的结构如下:
--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
);
当我用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
select*fromtb_evt_mail_clct t
wheret.clct_date = trunc(sysdate - 1)
andt.sender_cust_codelike'340104%'
当用户什么都不输入需要查询昨天所有邮件信息时,下面的语句并不能查询到所有信息,这条语句只能查到所有大客户的邮件信息,查不到散户的邮件信息:
[sql]
view plaincopy
select*fromtb_evt_mail_clct t
wheret.clct_date = trunc(sysdate - 1)
andt.sender_cust_codelike'%'
这是因为散户的客户代码为空值,下面这条语句可以同时兼顾上面两种情形(假定用0000代表空值):
有限定值:
[sql]
view plaincopy
select*fromtb_evt_mail_clct t
wheret.clct_date = trunc(sysdate - 1)
andnvl(t.sender_cust_code,'0000')like'340104%'
无限定值:
[sql]
view plaincopy
select*fromtb_evt_mail_clct t
wheret.clct_date = trunc(sysdate - 1)
andnvl(t.sender_cust_code,'0000')like'%'
限定值是0000时结果是所有散户:
[sql]
view plaincopy
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
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结果为真。
本文原创发布php中文网,转载请注明出处,感谢您的尊重!