我有这个查询,我在我的DAO类中建立后,从te用户输入。它基本上是一个搜索功能,有大约10个选项用户提供..我写了下面的查询,
select SQL_CALC_FOUND_ROWS *
from `database`.`table`
WHERE (Date(ctimestamp) BETWEEN '2011-12-02' and '2012-12-06') and (crent BETWEEN '' and '')
and (cimg1 IS NOT NULL or cimg2 IS NOT NULL or cimg3 IS NOT NULL or cimg4 IS NOT NULL or cimg5 IS NOT NULL or cimg6 IS NOT NULL or cimg7 IS NOT NULL or cimg8 IS NOT NULL)
and cflag = 1 and ctitle LIKE '%Testing%'
order by Date(ctimestamp) desc Limit 0, 100
所有参数都来自前端。现在这个查询我已经接近13条记录,参数和条件相同,但是当我运行mysql客户端时,它给出了0条记录。
此查询正确地为我提供了数据
select SQL_CALC_FOUND_ROWS *
from `database`.`table`
WHERE (Date(ctimestamp) BETWEEN '2011-12-02' and '2012-12-06')
and (cimg1 IS NOT NULL or cimg2 IS NOT NULL or cimg3 IS NOT NULL or cimg4 IS NOT NULL or cimg5 IS NOT NULL or cimg6 IS NOT NULL or cimg7 IS NOT NULL or cimg8 IS NOT NULL)
and cflag = 1 and ctitle LIKE '%Testing%'
order by Date(ctimestamp) desc Limit 0, 100
两者之间的区别是这部分
and (crent BETWEEN '' and '')
现在我怀疑当用户计划跳过这些字段时如何处理这种情况。了解一些见解。
答案 0 :(得分:1)
crent可能是一个数值。你问它是否在两个文本字段之间。这种情况永远不会成真。
答案 1 :(得分:0)
在程序中添加输入参数的验证。当它为NULL时,不要把它放在sql中。