mysql - orderBy 和 group By 查询

标签 mysql doctrine symfony-1.4 doctrine-1.2 doctrine-query

我的数据库条目

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/

相关文章:

php - 按价格对产品排序

php - 设置 PHP mysql 连接的最佳实践

php - 在 SQL 中计算级联价格规则

symfony1 - symfony 统计用户数

python - Django session 中的当前字段值

mysql - Magento - 谁通过数据库查询在 Magento 网上商店订购了什么?

php - Doctrine ORM CLI 工具不工作

symfony-1.4 - Composer 不在 AWS Ubuntu 服务器上工作,在本地工作正常

routing - 重定向模块/ Action /到模块/ Action

doctrine - 如何在 Doctrine 查询 symfony 1.4 中使用联合?