Zend framework Zend_Db_Select存在

Zend framework Zend_Db_Select存在,zend-framework,zend-db-select,Zend Framework,Zend Db Select,我的查询的一部分是存在条件: $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` <= '

我的查询的一部分是存在条件:

$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'] .
   ')'
);
$select->where(
'存在('。
'从'property\u price'中选择'price\u property\u id'。
'其中'price\u property\u id`='pu\u property\u id`'。
'和'price\u value`>='。$params['price\u min']。

'和'price\u value`据我所知,
Zend\u Db\u Select
没有在
where
子句中包含子查询的特定方法。我认为您无法进一步改进它的编写方式

您可以将子查询重写为
外部联接
,并且可以利用更多的
Zend\u Db\u Select
方法,但我不确定这样做的好处,因为您的代码工作正常且易于阅读


希望这能有所帮助,

我相信这就是你要找的

<?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)
));

?>

我创建我的子查询和子选择作为新的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.')');
$subQuery=new Zend_Db_Select();
$subQuery->from(数组('price'=>'property\u price'))
->其中('price\u property\u id=pu\u property\u id')
->其中('price\u value>=?',$params['price\u min']))
->其中('price_value
$select->where('EXISTS('.$subQuery.'));
失败,因为$subQuery不是字符串(至少在ZF2中)