所以我想知道这是不是管理这个表的数据的最好方法,因为我的查询没有得到我想要的正确结果。
我希望它能从表中的最后一个日期条目中抓取top5排名。问题是如果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 ) )编辑:还有谁想试一试?
如此迷失,仍然得不到想要的结果
发布于 2011-10-25 00:55:31
不确定它是什么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();发布于 2011-10-25 00:55:46
您可能希望按top5.date_created降序排序。如果你真的只想要最后一天,你还需要一个WHERE条件。
发布于 2011-10-25 01:10:53
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();
}https://stackoverflow.com/questions/7879037
复制相似问题