我需要为交叉表查找具有order by和group by选择的findManyToManyRowset。
用于交叉点子选择的SQL如下所示:
$order = "select *
from intersection
order by someCol desc";
$group = "select order.*
from ($order) as order
group by order.otherCol";结果子选择字符串为$group。
findManyToManyRowset的第五个参数$select应该是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)中所做的一切
发布于 2012-03-20 15:46:22
问题解决了。
我更改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;因此,在不使用order by和不使用join的情况下,我得到了相同的结果。
Zend code:
$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);https://stackoverflow.com/questions/9771409
复制相似问题