我正在尝试使用 Zend_Db_Select 编写一个选择查询,看起来有点像这样:

SELECT * FROM bar WHERE a = 1 AND (b = 2 OR b = 3)

然而,当使用where()和orWhere()的组合时,似乎不可能像上面那样使用条件分组。

Zend Framework 中是否有任何本机方法可以实现上述目标(无需编写实际查询?)

有帮助吗?

解决方案

手册 (示例 11.61。带括号的布尔表达式示例)


// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (price < 100.00 OR price > 500.00)
//     AND (product_name = 'Apple')

$minimumPrice = 100;
$maximumPrice = 500;
$prod = 'Apple';

$select = $db->select()
             ->from('products',
                    array('product_id', 'product_name', 'price'))
             ->where("price < $minimumPrice OR price > $maximumPrice")
             ->where('product_name = ?', $prod);

其他提示

上面的参考很好,但是如果你用弦乐演奏怎么办?

这是上面带有字符串的示例......

// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (product_name = 'Bananas' OR product_name = 'Apples')
//     AND (price = 100)

$name1 = 'Bananas';

$name2 = 'Apples';

$price = 100;

$select = $db->select()

->from('products',
                    array('product_id', 'product_name', 'price'))

->where("product_name = '" . $name1 . "' OR product_name = '" . $name2 . "'")

->where("price=?", $price);

我希望这有帮助。我花了一些时间才让琴弦正常工作。

干杯。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top