dongpengyu1363 2012-10-18 15:03
浏览 24
已采纳

使用postgres在zend框架中按日期分组

So I have the following query:

public function findByDateCreated($date){
        $select = $this->select();
        $select->where('dtcreated = ?', $date);
        $select->group('date');
        $rows = $this->fetchAll($select);
        if(count($rows) > 0){
            return $rows[0];
        }else{
            return false;
        }
    }

Which throws the error:

500: SQLSTATE[42703]: Undefined column: 7 ERROR: column "date" does not exist LINE 1: ...E (dtcreated = '2012-10-17 14:26:41.575479') GROUP BY "date" ^

My question is:

How do I group by date such that I get:

17-10-2012
One item
two item
three item

16-10-2012
oneitem
twoitem
threeitem

and so on?

I don't see how this is of any help but:

create table users_notifications(
    id  int primary key default nextval('users_notifications_id_seq') not null,
    userid int not null references users(id),
    itemid int not null,
    itemtype varchar(75),
    dtcreated timestamp not null default now(),
    dtupdated timestamp default now(),
    message text,
    flag boolean,
    flagnew boolean
);

that's how the table looks.

  • 写回答

1条回答 默认 最新

  • dsy6639 2012-10-18 15:38
    关注

    The error is because you don't have a column called date so you can't group by that.

    Try this:

    $select->group(new Zend_Db_Expr("date_trunc('hour', dtcreated)"));
    

    This will truncate all datetime's to just the year, month, day component, and then group by those resulting values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度