我需要選擇最高得分者作爲月度獲勝者。我也想展示以前的獲獎者。我目前的查詢僅選擇前一個月的獲勝者,但我如何選擇以前的所有月度獲勝者?選擇每個月最高分的獲勝者
我的查詢:
function month_winner_now()
{
$query = $this->db->query("SELECT winner.id,winner.score,winner.user_id,winner.date, user.id,user.username,user.email,user_profile.user_image,user_profile.hometown,user_profile.country FROM `winner` LEFT JOIN `user` ON user.id = winner.user_id LEFT JOIN `user_profile` ON user_profile.user_id = winner.user_id WHERE MONTH(CURDATE())= MONTH(winner.date) ORDER BY winner.score DESC
LIMIT 1");
return $query->result_array();
}
My current output :
"monthly winners":[
{
"id":"5",
"score":"1256",
"user_id":"5",
"date":"2014-03-05",
"username":"",
"email":"",
"user_image":"",
"hometown":"",
"country":""
}
但我需要輸出像
"monthly winners":[
{
"id":"4",
"score":"233",
"user_id":"4",
"date":"2014-03-02",
"username":"Mahmudul Hasan Swapon",
"email":"",
"user_image":"",
"hometown":"",
"country":""
},
{
"id":"7",
"score":"123",
"user_id":"7",
"date":"2014-03-04",
"username":"Prosanto Biswas",
"email":"",
"user_image":"",
"hometown":"",
"country":""
}
],
每月優勝者JSON數組顯示所有以前的一個月贏家,但每個月都應該有一個贏家。
數據庫表的樣子
id | name | userid | score | date |
------------------------------------------------------------
1 | john | 1 | 44 | 2013-03-2
2 | mary | 2 | 59 | 2013-03-5
3 | john | 12 | 38 | 2013-03-8
4 | elvis | 3 | 19 | 2013-02-10
5 | john | 11 | 1002 | 2013-01-11
6 | johnah | 10 | 200 | 2013-01-11
查詢有錯誤對你有所幫助 #1064 - 你在你的SQL錯誤句法;檢查對應於您的MySQL服務器版本的手冊,以在'1 MONTH))= MONTH(winner.date)ORDER BY winner.score DESC LIMIT 1'附近使用正確的語法 – user3407569
Ye,編輯,忘記了INTERVAL。 – sunshinejr