CodeIgniter中的组计数

时间:2016-06-28 12:24:51

标签: codeigniter

我要显示预订数量最多的前9个地方。所以我必须有$this->db->group_by('t.id_pl')的计数,然后是该计数的订单。

我设法做到了这一点,但我不明白我应该把计数放在哪里:

function select_pop ()
{
    $this->db->select( 's.place as place, s.price as price, s.title as title' );            
    $this->db->from('Places AS s'); 
    $this->db->join('Reservations AS t', 't.id_pl= s.place', 'INNER');       
    $this->db->group_by('t.id_pl');
    $this->db->order_by(''); 
    $this->db->limit(9);    
    $result = $this->db->get();
    return $result; 
}  

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以在select语句中添加计数,如下所示 -

$this->db->select('count(t.id_pl) as reservation_count,s.place as place, s.price as price, s.title as title');

然后按顺序你可以使用 -

$this->db->order_by('reservation_count','DESC');

我希望这会对你有帮助..