2017-05-31 109 views
0

如何在codeigniter中编写以下MySQL查询。我测试了查询并按预期工作。 select p.post_title, count(d._id) from posts p INNER JOIN discussions d ON p._id = d.post_id INNER JOIN accounts a ON a._id = p.account_id where a._id = '1494900911hRs5kjPXV9591a60afa434f' group by p._id 如何在Codeigniter查询中编写连接选择查询

+0

只需使用'$这个 - > DB->查询(“您所查询的HERE “)'。来自[docs](https://www.codeigniter.com/user_guide/database/queries.html)。 – Tpojka

回答

0

当你看到文档here可以使用查询生成器类来构建查询。所以你的查询生成器看起来就像这样:

$this->db->select('p.post_title, count(d._id)'); 
$this->db->from('post p'); 
$this->db->join('discussions d', 'p._id = d.post_id', 'inner'); 
$this->db->join('accounts a', 'a._id = p.account_id', 'inner'); 
$this->db->where('a._id', '1494900911hRs5kjPXV9591a60afa434f'); 
$this->db->group_by('p._id'); 
$query = $this->db->get(); 

然后就可以看到文档here生成查询结果

// get result. 
$rows = $query->result();