如何在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
答案 0 :(得分: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();
答案 1 :(得分:0)
$this->db->select('p.post_title, count(d._id) as count')->from('post p')->join('discussions d', 'p._id = d.post_id', 'inner')->join('accounts a', 'a._id = p.account_id', 'inner')->where('a._id', '1494900911hRs5kjPXV9591a60afa434f')->group_by('p._id');
$query = $this->db->get();
获取结果数据
$rows = $query->result(); // object format
$rows = $query->result('array'); // Array Format