SQL模糊查询碰到空值怎么办?
2017-05-04 09:41
363 查看
作者:iamlaosong
SQL查询语句用%来做模糊查询。程序中一般要求用户输入部分信息,依据这个信息进行模糊查询。
比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全部邮件信息:
当用户什么都不输入须要查询昨天全部邮件信息时,以下的语句并不能查询到全部信息,这条语句仅仅能查到全部大客户的邮件信息,查不到散户的邮件信息:
这是由于散户的客户代码为空值。以下这条语句能够同一时候兼顾上面两种情形(假定用0000代表空值):
有限定值:
无限定值:
限定值是0000时结果是全部散户:
除了like。not like、not in 、<>等运算符号也都不包括空值,比如以下语句并不包括客户代码为空值的记录:
要想包括。相同须要将条件改为:nvl(t.sender_cust_code,'0000') <> '34122600200300'
总之。当一个字段为空值时。表达式中不管是等于还是不等于,结果都为假,仅仅有 is null结果为真。
SQL查询语句用%来做模糊查询。程序中一般要求用户输入部分信息,依据这个信息进行模糊查询。
比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全部邮件信息:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and t.sender_cust_code like '340104%'
当用户什么都不输入须要查询昨天全部邮件信息时,以下的语句并不能查询到全部信息,这条语句仅仅能查到全部大客户的邮件信息,查不到散户的邮件信息:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and t.sender_cust_code like '%'
这是由于散户的客户代码为空值。以下这条语句能够同一时候兼顾上面两种情形(假定用0000代表空值):
有限定值:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '340104%'
无限定值:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '%'
限定值是0000时结果是全部散户:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '0000%'
除了like。not like、not in 、<>等运算符号也都不包括空值,比如以下语句并不包括客户代码为空值的记录:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and t.sender_cust_code <> '34122600200300'
要想包括。相同须要将条件改为:nvl(t.sender_cust_code,'0000') <> '34122600200300'
总之。当一个字段为空值时。表达式中不管是等于还是不等于,结果都为假,仅仅有 is null结果为真。
相关文章推荐
- SQL模糊查询碰到空值怎么办?
- SQL模糊查询碰到空值怎么办?
- mybatis中怎么使用like形成动态模糊查询,错误使用CONCAT拼接sql的看过来
- SQL Server之 (二) SQL语句 模糊查询 空值处理 聚合函数
- PB数据窗口sql语句中like后面怎么跟变量模糊查询
- 预声明中的模糊查询sql语句(PreparedStatement like sql)怎么写
- 模糊查询/空值处理/数据排序oreder by/数据分组group by/SQL语句执行顺序
- SQL模糊查询
- sql特殊字符的模糊查询处理
- SQL 模糊查询
- 关于int型数据的SQL模糊查询猜想
- mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL
- SQL 模糊查询
- SQL 模糊查询 [0-9,a-z], 及Tab, 回车, 换行, 单引号, 双引号
- 多字段模糊查询sql like %% 优化与区别
- SQL 模糊查询
- SQL 模糊查询,正则查询
- sql对xml的模糊查询
- sql语句的模糊查询复习及小技巧一个
- 详解传参时的SQL多条件模糊查询(VS2012)