2011-11-16 51 views
1

一部分是EXISTS條件:Zend_Db_Select對象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框架?

回答

0

據我所知,Zend_Db_Select沒有具體的方法在where子句中包含子查詢。我認爲你無法進一步提高它的寫作方式。

您可能會將子查詢重寫爲OUTER JOIN,並且您可以利用更多Zend_Db_Select方法,但我不確定這樣做的好處,只要您的代碼工作正常並且清晰可讀即可。

希望幫助,

+0

這是一個可惜:(謝謝! –

0

我相信這是你在找什麼!:

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

創建我的子查詢和子選擇作爲新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.')');