dongliling6336 2011-11-16 14:34
浏览 63
已采纳

使用聚合(分组)函数进行CGridView过滤 - 例如MAX()

I have a CGridView that uses a MAX() mysql column to provide data for one of the columns. I have that working with sorting, but I can't figure out filtering. I assumed that I could just use CDbCriteria::compare() call to set it, but it's not working. Ideas?

My search function:

    $criteria = new CDbCriteria;
    $criteria->condition = 't.is_deleted = 0 and is_admin = 0';
    // get last note date
    $criteria->select = array('t.*', 'MAX(n.visit_date) AS last_note');
    $criteria->join = 'LEFT JOIN notes n ON t.id = n.distributor_id';
    $criteria->group = 't.id';
    $criteria->order = 't.status';

    $criteria->compare('t.type', $this->type);
    $criteria->compare('t.name', $this->name, true);
    $criteria->compare('t.city', $this->city, true);
    $criteria->compare('t.state', $this->state);
    $criteria->compare('last_note', $this->last_note);



    return new CActiveDataProvider('Distributor', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20
            ),
            'sort' => array(
                'defaultOrder' => 'name',
                'attributes' => array(
                    'last_note' => array(
                        'asc' => 'last_note',
                        'desc' => 'last_note DESC'
                    ),
                )
            ),
        ));

In my view, I just have the name and value values set.

The error I get is CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_note' in 'where clause'

Edit: The only way I found to do this is (because you can't have aggregate functions in where clauses) is to check the $_GET array for the last_note value, and add a having clause. Note, this isn't parameterized or anything yet, I just wanted to rough it out to see if it would work:

if(isset($_GET['Distributor']['last_note']) && $_GET['Distributor']['last_note'] != '') {
        $criteria->having = 'MAX(n.visit_date)=\'' . $this->last_note . "'";
}

I hate using the request variables in the model like this, but there isn't much else I could do.

  • 写回答

2条回答 默认 最新

  • doubingqi5829 2011-11-16 16:40
    关注

    You're on the right track. You filter aggregate functions like MAX() and SUM() using having. In a query like this, you should probably add a group, otherwise you'll end up with a lot of duplicated data in your result when a Distributor has multiple notes on the same day (assuming t.id is a primary key). Second, you should use named params instead of putting variables directly into SQL. So, instead of compare('last_note'... you'd end up with something like this:

    $criteria->group = 't.id';
    $criteria->having = 'MAX(n.visit_date) = :last_note';
    $criteria->params = array(':last_note' => $this->last_note);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘