【问题标题】:Zend Framework Select Operator PrecedenceZend 框架选择运算符优先级
【发布时间】:2008-09-17 07:38:23
【问题描述】:
我正在尝试使用 Zend_Db_Select 编写一个看起来有点像这样的选择查询:
SELECT * FROM bar WHERE a = 1 AND (b = 2 OR b = 3)
但是,当使用 where() 和 orWhere() 的组合时,似乎不可能像上面那样使用条件分组。
Zend 框架中是否有任何本地方式来实现上述功能(无需编写实际查询?)
【问题讨论】:
标签:
php
mysql
zend-framework
【解决方案1】:
来自the manual(示例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);
【解决方案2】:
上面的参考很好,但是如果你玩的是弦乐呢?
这将是上面带有字符串的示例...
// 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);
我希望这会有所帮助。花了我一些时间让字符串正常工作。
干杯。