所以我想知道这是否是管理此表数据的最佳方式,因为我的查询得不到我想要的正确结果。
我希望它能从表格中的最后一个日期条目中获得前5名排名。问题是如果character_id为0。当我执行我的php以查看是否没有值时,它应该回显TBD,但它仍然没有在数组中显示为0。
表:top5
id ranking # character_id status_id date_created
1 1 1 1 2011-10-17 17:18:54
2 2 2 1 2011-10-17 17:18:54
3 3 3 1 2011-10-17 17:18:54
4 4 4 1 2011-10-17 17:18:54
5 5 5 1 2011-10-17 17:18:54
6 1 6 1 2011-10-24 12:18:54
7 2 7 1 2011-10-24 12:18:54
8 3 8 1 2011-10-24 12:18:54
9 4 9 1 2011-10-24 12:18:54
10 5 0 1 2011-10-24 12:18:54
function getTop5()
{
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->where('top5.status_id', '1');
$this->db->order_by('top5.date_created','desc');
$this->db->limit(5);
$query = $this->db->get();
return $query->result_array();
}
Array ( [0] =>
Array ( [character_name] => \"Mr. Magnificent\" Matt Sharp
[character_id] => 9 )
[1] =>
Array ( [character_name] => \"The Unforgettable\" Jimmy Watkins
[character_id] => 8 )
[2] =>
Array ( [character_name] => Romie Rains
[character_id] => 7 )
[3] =>
Array ( [character_name] => Monica Dawson
[character_id] => 6 )
[4] =>
Array ( [character_name] => \"The Outlaw\" Mike Mayhem
[character_id] => 5 ) )
编辑:其他人想尝试一下吗?
如此迷失,仍然无法获得理想的结果
答案 0 :(得分:1)
不确定它是什么db类,只是附加
AND date(date_created) = CURDATE();
查询
或将DESC
追加到ORDER
子句
类似
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->where('top5.status_id', '1');
// WHERE date(top5.date_created) = CURDATE()
$this->db->order_by('top5.date_created');
$this->db->limit(5);
$query = $this->db->get();
答案 1 :(得分:1)
您希望按top5.date_created
降序排序。如果你真的只想要最后一天,那么你也需要一个WHERE条件。
答案 2 :(得分:1)
function getTop5()
{
$this->db->select('characters.character_name, top5.character_id');
$this->db->from('top5');
$this->db->join('characters', 'characters.id = top5.character_id');
$this->db->join( '( SELECT DATE(MAX(date_created)) AS lastdate
FROM top5
WHERE status_id = 1
) AS tm'
, 'top5.created_at >= tm.lastdate
AND top5.created_at < tm.lastdate + INTERVAL 1 DAY');
$this->db->where('top5.status_id', '1');
$this->db->order_by('top5.ranking','asc');
$this->db->limit(5);
$query = $this->db->get();
return $query->result_array();
}