首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排名前5的问题

排名前5的问题
EN

Stack Overflow用户
提问于 2011-10-25 00:52:01
回答 3查看 121关注 0票数 1

所以我想知道这是不是管理这个表的数据的最好方法,因为我的查询没有得到我想要的正确结果。

我希望它能从表中的最后一个日期条目中抓取top5排名。问题是如果character_id的值为0。当我执行php时,如果没有值,那么它应该回显TBD,但它仍然没有在数组中显示它为0。

表: top5

代码语言:javascript
复制
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 ) )

编辑:还有谁想试一试?

如此迷失,仍然得不到想要的结果

EN

回答 3

Stack Overflow用户

发布于 2011-10-25 00:55:31

不确定它是什么db类,但只需追加

代码语言:javascript
复制
AND date(date_created) = CURDATE();

添加到查询

或将DESC附加到ORDER子句

所以就像这样

代码语言:javascript
复制
$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
EN

Stack Overflow用户

发布于 2011-10-25 00:55:46

您可能希望按top5.date_created降序排序。如果你真的只想要最后一天,你还需要一个WHERE条件。

票数 1
EN

Stack Overflow用户

发布于 2011-10-25 01:10:53

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7879037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档