如何在 Codeigniter 中加入三个表 [英] How to JOIN three tables in Codeigniter
问题描述
我正在使用 codeigniter 框架开发一个音乐 cms.我在 mysql 数据库中有 3 个表,目前我在相册"表和模型、控制器"中工作.我想选择专辑"表1并加入专辑"->cat_id"与类别"->cat_id",并获取所有类别记录.
I'm using codeigniter framework to develop one music cms. i have 3 tables in mysql database, Currently im working in "Album" Table and "Model, Controller". i want to SELECT "Album" Table 1 and JOIN "Album" -> "cat_id" with "Category" -> "cat_id", and fetch all categories records.
然后我想加入专辑"->原声带"上的album_id"->album_id"然后获取所有原声带记录A到Z.
Then i want to JOIN "Album" -> "album_id" on "Soundtrack" -> "album_id" then fetch all soundtrack records A to Z.
请有人帮我展示正确的 codeigniter 查询,我如何选择和加入表然后从 3 个表中获取记录,?
Please somebody help me to show proper codeigniter query, how i can SELECT and JOIN Tables then fetch records from 3 tables, ?
表 1 -> 类别
cat_id
cat_id
cat_name
日期
表 2 -> 相册
专辑编号
专辑名称
专辑详情
表 3 -> 原声带
专辑编号
track_title
track_title
推荐答案
在模型中使用此代码
public function funcname($id)
{
$this->db->select('*');
$this->db->from('Album a');
$this->db->join('Category b', 'b.cat_id=a.cat_id', 'left');
$this->db->join('Soundtrack c', 'c.album_id=a.album_id', 'left');
$this->db->where('c.album_id',$id);
$this->db->order_by('c.track_title','asc');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result_array();
}
else
{
return false;
}
}
这篇关于如何在 Codeigniter 中加入三个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!