zend findManyToManyRowset subselect group by

时间:2012-03-19 13:49:38

标签: zend-framework group-by subquery

我需要找到带有order by的findManyToManyRowset和用于交叉表的选择。

交叉子选择的SQL如下所示:

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

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

结果子选择字符串是$ group。

第五个参数$ select of findManyToManyRowset应该是Zend_Db_Table_Select,但是我坚持这个点,因为我不能用Zend_Db_Table_Select做真正的子选择,因为在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)

中所做的一切

1 个答案:

答案 0 :(得分: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);