0

让我先解释一下这个问题,然后再解释一下我对这个问题的一些解决方案。(肯定会遇到很多人,但我不知道如何用干净的代码解决它)

Q:有一个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);

或者第二种方式

创建多个函数并通过它们传递数据。

哪个是更好的实现?有没有相同的文章?

4

1 回答 1

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.

于 2012-10-18T14:44:25.350 回答