2012-03-19 59 views
1

我需要找到findManyToManyRowset與秩序和組選擇交集表。zend findManyToManyRowset subselect group by

SQL交叉口子選擇的模樣:

$order = "select * 
from intersection 
order by someCol desc"; 

$group = "select order.* 
from ($order) as order 
group by order.otherCol"; 

結果子選擇字符串爲$組。

第五個參數$選擇findManyToManyRowset應該是Zend_Db_Table_Select對象,但我被困在這個POIN,becouse我不能做真正的分與Zend_Db_Table_Select對象和becouse在findManyToManyRowset選擇(..) 有串

if ($select === null) { 
    $select = $matchTable->select(); 
} else { 
    $select->setTable($matchTable); 
} 

    $select->from(array('i' => $interName), array(), $interSchema) 
      ->joinInner(array('m' `enter code here`=> $matchName), $joinCond, Zend_Db_Select::SQL_WILDCARD, $matchSchema) 
      ->setIntegrityCheck(false); 

他們打破了我所做的$ select($ group)

回答

0

問題解決了。

心中已經改變SQL字符串this way

select 
* 
from someTable st1 
where groupCol = 
    (
    select 
     max(st2.groupCol) 
    from someTable st2 
    where 
     st1.firstId=st2.firstId 
     and st1.secondId=st2.secondId 
     .... 
     and etc 
    ) 
--and groupResult=1; 

,所以我有相同的結果,而不取決於訂單子查詢和無連接的。

Zend公司代碼:

$matchTable,$intersectionTable - should be instance of Zend_Db_Table_Abstract with correct reference map 

     $max = $intersectionTable->select() 
       ->from(array('mx' => 'intersectionTableName'), new Zend_Db_Expr("max(mx.groupCol)")) 
       ->where('i.firstId = mx.firstId') 
       ->where('i.secondId = mx.secondId') 
       ... 
       -> etc ; 


     $select = $matchTable->select() 
//     ->where('i.groupResult = ?', true) optional, in case we would like to filter result rowset by grouped value 
        ->where('i.groupCol = ?', $max); 



     $result = $row->findManyToManyRowset($matchTable, $intersectionTable, null, null, $select);