1

我的查询的一部分是 EXISTS 条件:

$select->where(
  'EXISTS(' .
  'SELECT `price_property_id` FROM `property_price` ' .
    'WHERE `price_property_id` = `pu_property_id`' .
      ' AND `price_value` >= ' . $params['price_min'] .
      ' AND `price_value` <= ' . $params['price_max'] .
   ')'
);

它是如何在 Zend Framework 中以正确的方式编写的?

4

3 回答 3

2

我将子查询和子选择创建为新的 Zend_Db_Select 对象。这使代码更简洁,因为我可以在其他地方重用该查询,它还有助于调试,因为我可以echo (string)$subQuery查看 SQL 的那部分。

$subQuery = new Zend_Db_Select();
$subQuery->from(array('price' => 'property_price'))
     ->where('price_property_id = pu_property_id')
     ->where('price_value >= ?', $params['price_min'])
     ->where('price_value <= ?', $params['price_max']);

$select->where('EXISTS('.$subQuery.')');
于 2015-09-10T17:08:59.353 回答
1

我相信这就是你要找的东西!:

<?php

// $select is instance of Zend_Db_Select
// $db is instance of Zend_Db_Adapter

$select->where('exists (?)', new Zend_Db_Expr(
    $db->quoteInto('select * from your_table where id = ?', $id, Zend_Db::PARAM_INT)
));

?>
于 2012-08-03T05:15:41.820 回答
0

据我所知,Zend_Db_Select没有特定的方法可以在where子句中包含子查询。我认为你不能进一步改进它,它的编写方式。

您可以将子查询重写为OUTER JOIN,并且您可以利用更多Zend_Db_Select方法,但我不确定这样做的好处,只要您的代码正常工作且易于阅读。

希望有帮助,

于 2011-11-16T09:27:19.330 回答