如何在codeigniter Active Record表单中编写mysql查询?

时间:2015-04-07 08:57:33

标签: mysql codeigniter

我想以codeigniter Active Record

的形式编写下面的mysql查询
select c.*
from code c
left join code_like l on c.id = l.code_id
group by c.id
order by count(*) desc

我正在尝试下面的查询,但如何添加order by count(*) desc

        $this->db->select('*');
        $this->db->from('code');
        $this->db->join('code_like', 'code.id = code_like.id');
        $this->db->group_by("code.id"); 
        $query = $this->db->get();

1 个答案:

答案 0 :(得分:6)

这对您有用,因为您的代码中存在拼写错误

$this->db->select('c.*,count(c.*) as data');
$this->db->from('code c');
$this->db->join('code_like cl', 'c.id = cl.code_id','left');
$this->db->group_by('c.id');
$this->db->order_by('data DESC');
$result = $this->db->get->result_array();
print_r($result);