如何从表中找到同一用户的最新记录?

时间:2011-01-03 07:52:00

标签: mysql cakephp jqgrid mysql-error-1054

我正在开发一个蛋糕php应用程序。在这里我使用jqgrid来显示我的记录。

这是我的ctp文件:

    <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#list").jqGrid(
        {
            url:'<?php if(!empty($this->params["named"]["batch"]))echo $this->Html->url(array("controller" => "placements", "action" => "report17", "ajax" => true,"batch"=>$this->params["named"]["batch"])); else echo $this->Html->url(array("controller" => "placements", "action" => "report17", "ajax" => true)); ?>',
            datatype: "json",
            mtype: "GET",
            colNames:['Student','Phone No.','Batch','Created'],
            colModel:[
                {name:'studentname',index:'studentname', width:30, search:true},
                {name:'contactnumber1',index:'contactnumber1', width:20, search:true},
                {name:'batchname',index:'batchname', width:30, search:true},
                {name:'latest',index:'latest', width:30, search:true},

            ],
            rowNum:10,
            rowList:[10,20,30],
            pager: jQuery('#pager'),
            sortname: 'latest',
            viewrecords: true,
            sortorder: "desc",
            caption:"Placements",
            height:"auto",
            autowidth: true,
            navigator:true
        });
        jQuery("#list").navGrid("#pager",{edit:false,add:false,del:false,search:false});
        jQuery("#list").jqGrid('filterToolbar');
    })
</script>

这是我在控制器中定义的函数

 $conditions = array();
            if(!empty($this->params['named']['batch']))
            array_push(&$conditions, array('Batch.id' => $this->params['named']['batch'] ));
            array_push(&$conditions, array('Student.type' => 'student' ));
            array_push(&$conditions, array('Student.trainingcompleted' => '1' ));
            $result = $this->Placement->find('all', array(
                'fields' => array('id','student_id','company_id','branch_id','max(Placement.created) as latest'),
                'link' => array(
                    'Student' => array(
                        'Batch'
                    ),
                    'Company'
                ),
                'group'=>'Placement.student_id',
                'conditions'=>$conditions,
                'order' => $sort_range,
                'limit' => $limit_range
            ));
        }

        $i = 0;
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;

        //pr($result);

        foreach($result as $result)
        {
            $response->rows[$i]['id'] = $result['Placement']['id'];

            $student = "<a href='" . APP_URL . "students/view/" . $result['Student']['id'] . "/by:student'>" . $result['Student']['fullname'] . "</a>";
            $batch = "<a href='" . APP_URL . "batches/view/" . $result['Batch']['id'] . "'>" . $result['Batch']['name'] . "</a>";
            $contactnumber1 =$result['Student']['contactnumber1'];

            $response->rows[$i]['cell'] = array($student, $contactnumber1, $batch,$result['Placement']['latest']);
            $i++;
        }

        echo json_encode($response);

在展示位置表中,学生可能会有多个条目。

例如

id student_id istillworking created
 1   16        no           2010-09-07 10:02:16
 2   16        yes          2010-12-30 12:48:44

我想显示每个用户的最新记录。 例如,对于student_id 16,它应该获取在2010-12-30 12:48:44创建的记录。

我尝试通过上面的代码实现这一点。但我收到以下错误:

警告(512): SQL错误: 1054:'order clause'中的未知列'Placement.latest'

我无法解决问题。

请帮我解决这个问题

谢谢,

Pankaj Khurana

1 个答案:

答案 0 :(得分:0)

您的“展示位置”表中没有“最新”列,或者您没有在$ result变量中传递它。也许您已在find语句中指定了列,但未包含“最新”。

尝试调试($ result);在循环内。这将显示已传递的数据。

顺便说一下,做

并不是一个好主意
foreach($result as $result)
{
    ...
}

...除了在语法上不正确之外,以后可能会导致不可预测的行为。最好将结果作为$ results(复数)传递并执行

foreach($results as $result)
{
    ...
}