我的数据库条目
id | shop_id | last_changed
1 | 1 | 7 pm
2 | 1 | 8 pm
3 | 2 | 9 pm
4 | 2 | 8 pm
5 | 3 | 5 pm
我的查询应该是我只获取具有 id 的条目:
2, 3, 5
当我这样做时:
$q = Doctrine_Query::create()
->from('list e')
->groupBy('e.shopname_id')
->orderBy('e.last_changed DESC')
->limit(6)
->fetchArray();
我得到:1,4,5
。即使我执行e.last_changed ASC
,它也不起作用。
我只想要具有 last_changed
和不同 shop_id
的条目。我该怎么做?
谢谢!
最佳答案
试试这个:
$q = Doctrine_Query::create()
->select('e.shopname_id, MAX(e.id) AS id, MAX(e.last_changed) AS last_changed')
->from('list e')
->groupBy('e.shopname_id')
->orderBy('MAX(e.last_changed) DESC')
->limit(6)
->fetchArray();
关于mysql - orderBy 和 group By 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11601927/