2011-11-16 2 views
1

한 부분은 EXISTS EXISTS 조건 :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` <= ' . $params['price_max'] . 
    ')' 
); 

?

답변

0

알고있는 한 Zend_Db_Select에는 where 절에 하위 쿼리를 포함 할 구체적인 방법이 없습니다. 나는 당신이 그것을 더 이상 향상시킬 수 없다고 생각합니다.

하위 쿼리를 OUTER JOIN으로 다시 작성하면 Zend_Db_Select 개 이상의 메소드를 활용할 수 있지만 코드가 올바르게 작동하고 명확하게 읽히는 한 이점이 있는지는 잘 모르겠습니다. . 도움이

희망,

+0

감사합니다. (감사합니다. –

0

나는 이것이 당신이 내 서브 쿼리와 새로운 Zend_Db_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) 
)); 

?> 
1

찾고있는 것입니다 믿습니다. 이렇게하면 코드를 약간 더 깔끔하게 정리할 수 있으므로 디버깅에 도움이됩니다. 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.')');