我有一张数据表 数据库表格看起来像
id | name | userid | score | date |
------------------------------------------------------------
1 | john | 4 | 233 | 2014-02-02
2 | mary | 5 | 1256 | 2013-02-05
3 | john | 6 | 100 | 2013-03-08
4 | elvis | 7 | 123 | 2013-03-04
5 | john | 2 | 1234 | 2013-03-02
我当前的查询正确显示每个月的每月获胜者
"monthly_winners":[
{
"id":"2",
"score":"1256",
"month":"Feb"
},
{
"id":"5",
"score":"1234",
"month":"Mar"
}
],
但我希望根据最高分(周日至周日开始)显示每周获奖者名单最多两名获胜者,并显示当前结果
"monthly_winners":[
{
"id":"2",
"score":"1256",
"month":"Feb"
"weekly_winners":[
{
"id":"1",
"score":"1256",
},
{
"id":"2",
"score":"233","
},
],
},
{
"id":"5",
"score":"1234",
"month":"Mar"
"weekly_winners":[
{
"id":"5",
"score":"1234",
},
{
"id":"4",
"score":"123","
},
],
}
],
我正在使用codeigniter
我的控制器代码
public function month_winner()
{
if($win = $this->api_model->month_winner())
{
$msg = "OK";
}
else
$msg = "ERROR";
$output = array(
'msg' => $msg,
'monthly_winners' => $win,
);
jsonOutput($output);
}
我的型号代码
function month_winner()
{
$query=$this->db->query("x.id,x.score
JOIN
( SELECT DATE_FORMAT(date,'%Y%m')yearmonth
, MAX(score) max_score
FROM winner x
GROUP
BY DATE_FORMAT(date,'%Y%m')
) y
ON y.yearmonth = DATE_FORMAT(x.date,'%Y%m')
AND y.max_score = x.score");
return $query->result_array();
}