来自教义中的选择2

时间:2014-06-10 11:59:50

标签: doctrine-orm

如何在doctrine2 QB或DQL中执行此操作。

SELECT * FROM

( 按时间戳desc从my_table顺序中选择* )作为my_table_tmp

分组catid

按nid desc命令

1 个答案:

答案 0 :(得分:0)

我认为您的查询与以下内容相同:

SELECT *
FROM my_table
GROUP BY catid
    HAVING timestamp = MAX(timestamp)
ORDER BY nid DESC
;

如果它是正确的,那么你应该能够:

$qb->select('e')
    ->from('My\Entities\Table', 'e')
    ->groupBy('e.catid')
    ->having('e.timestamp = MAX(e.timestamp)')
    ->orderBy('nid', 'DESC')
;

或者,直接使用DQL:

SELECT e
FROM My\Entities\Table e
GROUP BY e.catid
    HAVING e.timestamp = MAX(e.timestamp)
ORDER BY e.nid DESC
;

希望这有帮助并且有效! ;)