相关疑难解决方法(0)

使用多个AND运算符的Zend_Db的复杂WHERE子句

我想在Zend_Db中生成这个复杂的WHERE子句:

SELECT * 
FROM 'products' 
WHERE 
    status = 'active' 
    AND 
    (
        attribute = 'one' 
        OR 
        attribute = 'two' 
        OR 
        [...]
    )
;
Run Code Online (Sandbox Code Playgroud)

我试过这个:

$select->from('product');
$select->where('status = ?', $status);
$select->where('attribute = ?', $a1);
$select->orWhere('attribute = ?', $a2);
Run Code Online (Sandbox Code Playgroud)

那产生了:

SELECT `product`.* 
FROM `product` 
WHERE 
    (status = 'active') 
    AND 
    (attribute = 'one') 
    OR 
    (attribute = 'two')
;
Run Code Online (Sandbox Code Playgroud)

我确实找到了一种方法来完成这项工作,但我觉得通过先使用PHP组合"OR"子句然后使用Zend_Db where()子句将它们组合起来,这有点"作弊".PHP代码:

$WHERE = array();
foreach($attributes as $a):
    #WHERE[] = "attribute = '" . $a . "'";
endforeach;
$WHERE = implode(' OR ', $WHERE);

$select->from('product');
$select->where('status …
Run Code Online (Sandbox Code Playgroud)

php sql zend-framework zend-db

15
推荐指数
2
解决办法
1万
查看次数

标签 统计

php ×1

sql ×1

zend-db ×1

zend-framework ×1