gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:39:36 25 4
gpt4 key购买 nike

我正在开发一个蛋糕 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>

这是我在 Controller 中定义的函数

 $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);

在 placements 表中,学生可能有多个条目。

例如

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 子句”中的未知列“Placement.latest”

我无法解决这个问题。

请帮我解决这个问题

谢谢,

潘卡吉库拉纳

最佳答案

您的“placements”表没有“latest”列,或者您没有在 $result 变量中传递它。也许您在查找语句中指定了列,但未包括“最新”。

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

顺便说一句,这不是一个好主意

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

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

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

关于mysql - 如何从表中查找同一用户的最新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4582828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com