您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-13 23:01

作者:iamlaosong

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%'






热门排行

今日推荐

热门手游