如何在Doctrine中查找groupwise最大值或包含最大值的行?在SQL中,我通常會使用自連接來執行此操作,如here所述。在Doctrine中查找Groupwise最大值
儘管可以在Doctrine中建立自我關係,有沒有更好的方法來做到這一點? GroupWise的最大的
如何在Doctrine中查找groupwise最大值或包含最大值的行?在SQL中,我通常會使用自連接來執行此操作,如here所述。在Doctrine中查找Groupwise最大值
儘管可以在Doctrine中建立自我關係,有沒有更好的方法來做到這一點? GroupWise的最大的
例子:
$query = Doctrine_Query::create()
->select("txs.id, txs.amount, txs.valid_from")
->from("Tx txs")
->where("txs.amount = (SELECT MAX(transact.amount) FROM tx transact WHERE txs.id = transact.id)");
包含行最大的例子:
$query = Doctrine_Query::create()
->select("txs.id, txs.amount, txs.valid_from")
->from("Tx txs")
->where("txs.id = (SELECT transact.id FROM tx transact WHERE transact.amount = (SELECT MAX(transactx.amount) FROM tx transactx))");
這些可能不是唯一的方式(或最乾淨的),但我只是測試了和他們的工作。
這個問題真的很老了,但它在Google上的排名高達「教條組最大」,所以我想我會添加我的解決方案。
在我的情況下,我有一個父母實體有很多孩子,我想從ID最高的孩子中選擇字段。
$qb
->select('child1.field')
->from(Entity::class, 'entity')
->join('entity.children', 'child1')
->leftJoin('entity.children', 'child2', 'WITH', 'child1.entity=child2.entity AND child1.id<child2.id')
->where('child2.id IS NULL')
;
任何你不想使用自我關係的原因? – DrColossos 2010-06-24 05:40:57
兩個原因:1)它似乎違反了ORM模型。也就是說,對象擁有自己的副本是沒有意義的。 2)我很少做這個查詢,所以它似乎不值得添加一個關係。 – 2010-06-24 13:24:11