今天遇到了一个数据的查询问题,首先介绍一下情况:假设我们有一个表中有个推荐字段叫做isBest 当isBest=0时是不推荐,当isBest=1时是推荐的,但是该字段在没有填写的情况下默认值为null。那如果我们想要查询没有被推荐的信息时,要注意isBest=0或者为null都应该为不推荐的所以在数据库查询时就要格外注意。
例如我们要查询不被推荐的信息数量:
select count(*) from tableName where isBest <>1
注意上面这种写法是错误的,要想实现上面的效果要使用如下语句:
select count(*) form tableName where isBest=0 or isBest is null
采用webwork的写法就是:
hql = hql + " and isBest=0 or isBest is null";
和
if (searchFaqForm.getChkbest() != null) {
if("1".equals(searchFaqForm.getSearchbest())){
crt.add(Restrictions.eq("isBest", Integer.valueOf(searchFaqForm.getSearchbest())));
}else{
crt.add(Restrictions.or(Restrictions.eq("isBest", Integer.valueOf("0")), Restrictions.isNull ("isBest")));
}
}