Codeigniter活动记录 - 查询3个表 [英] Codeigniter active record - query 3 tables
本文介绍了Codeigniter活动记录 - 查询3个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用3个表进行查询,我有一些麻烦。
我的项目中有3个表:
I need to make a query using 3 tables and i having some trouble with that. I have 3 tables in my project:
项目
, projects_categories
和类别
- id_project
- title
- 日期
projects_categories
- id_proj_cat
- id_project
- id_category
类别
- id_category
- name
我已经创建了一个连接查询,但结果是一个具有相同project_id的数组。
我需要的是一个更高效的查询,可以列出每个project_id一个数组里面,它的类别和名称。像这样的。
I already made a join query but the result is a array with the same project_id showing several times. What i need is a more efficient query that can list for each project_id a array inside, with it´s categories and names. Something like that.
我可以做一个单独的查询,但我试图在一个单独的查询中实现。
I can make a separate query but im trying to achieve that in one single query.
推荐答案
尝试这个:
$this->db->select('p.id_project,c.categories,c.name');
$this->db->from('projects p');
$this->db->join("projects_categories pc","p.id_project = pc.id_project","INNER");
$this->db->join("categories c","pc.id_category = c.id_category","INNER");
$query = $this->db->get();
return $query->result_array();
这篇关于Codeigniter活动记录 - 查询3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文