让我先解释一下这个问题,然后再解释一下这个问题。 (肯定会遇到很多人,但我不知道如何用干净的代码来解决它)
问:有一个mysql数据库,想要根据多个和/或where条件进行查询。 例如。其中column1 = 3,column2不喜欢“ab%”一个简单的解决方案(因为我使用的是php zend查询构建器)
if(column1)
$query->where(conditions);
if(column2)
$query->where(conditions);
.
.
.
if(columnn)
$query->join(some params)
->where(conditions);
或第二种方式
创建多个函数并通过它们传递数据。
哪个更好实现?有没有写过相同的文章?
答案 0 :(得分:0)
您的where
子句应如下所示:
where
(:p1 is null or col1 = :p1)
or (
(:p2 is null or col2 = :p2)
and
(:p3 is null or col3 like '%:p3')
)
:p
是参数。如果未给出该参数,则将其传递为null
。