sql 模糊查询like 如何处理 NULL
来源:互联网 发布:mac 提醒事项 红点 编辑:程序博客网 时间:2024/05/29 12:29
今天开发程序时遇到一个问题,我负责前台调用另一个同事写的后台查询方法。我只需要将查询值传给查询方法即可。
表中只有三个字段,表结构如下
deptnodeptnamedescript1aa无2bbNULLdeptno、deptname、descript三个字段都是查询条件(也就是where条件)
页面只输入了部门编号是2,其他两列没有值,则为空。
查询sql是 select * from dept where deptno like '%2%' and deptname like '%%'and descript like '%%'
因为用户只要找编号是2的记录,其他条件没有输入,而数据库也有这条记录,按理说应该能查询出来,但是因为descript是NULL,使用like '%%'无法查出NULL的记录,所以就没有查询出。
其实正确的处理方式应该是拼接sql
string sql = "select * from dept where 1=1";
if(deptno != "")
{
sql+= "and deptno like '%'"+deptno+"'%' ";
}
else if(deptname != "")
{
sql+= "and deptnamelike '%'"+deptname+"'%' ";
}
else if(descript != "")
{
sql+= "and descriptlike '%'"+descript+"'%' ";
}
另一种解决方法是利用isnull函数,但因为效率问题还是不赞成使用第二种方法
select * from dept where deptno like '%2%' and isnull(deptname,'') like '%%'and isnull(descript,0) like '%%'
- sql 模糊查询like 如何处理 NULL
- sql 模糊查询like 如何处理 NULL
- SQL like 模糊查询
- SQL like 模糊查询
- SQL like 模糊查询
- SQL like 模糊查询
- SQL like模糊查询
- SQL like 模糊查询
- SQL like 模糊查询
- SQL like 模糊查询
- SQL like模糊查询
- sql concat like 多字段模糊查询 其中有null
- SQL模糊查询语句(like)
- SQL模糊查询语法LIKE
- SQL 模糊查询(like)
- SQL模糊查询中的Like
- oracle sql语言模糊查询--通配符like
- mybatis like %% 模糊查询防sql 注入
- CoreData 实例 --- 通讯录
- HDU 4160 Dolls 最小路径覆盖
- Android---46---电话管理器 TelephonyManager
- css3学习笔记(一)
- Java Native Interface (JNI)
- sql 模糊查询like 如何处理 NULL
- Android 仿 iphone 自定义滚动选择器
- 哈理工 oj 1161 Leyni
- mysql中查询第几行到第几行的记录
- <leetcode>Add Two Numbers
- EditPlus 快速编译运行,激活
- Android开发 解决RecyclerView in SwipeRefreshLayout触发下拉刷新的bug
- hive中的join
- ORACLE 11G收缩表空间报错 ORA-03297: file contains used data beyondrequested RESIZE value