Zend Framework选择运算符优先级

时间:2020-03-05 18:58:12  来源:igfitidea点击:

我正在尝试使用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);

希望对我们有所帮助。让我有些鬼混,以使字符串正常工作。

干杯。