如何在Codeigniter中加入三个表 [英] How to JOIN three tables in Codeigniter
问题描述
我使用codeigniter框架来开发一个音乐cms。我有3个表在mysql数据库,目前工作在相册表和模型,控制器。我想选择相册表1和JOIN相册 - >cat_id与类别 - >cat_id,并获取所有类别记录。
然后我想在Soundtrack - >album_id上加入Album - >album_id,然后获取所有音轨记录A到Z。
请有人帮助我显示正确的代码信号查询,我如何可以SELECT和JOIN表然后从3个表中获取记录?
表1 - >类别 p>
-
cat_id
-
cat_name
>
- cat_title
-
日期
表2 - >相簿
-
-
album_details
表3 - > Soundtrack
- track_url
- 日期
public function funcname($ id)
{
$ this-> db->选择('*');
$ 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;
}
}
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.
Then i want to JOIN "Album" -> "album_id" on "Soundtrack" -> "album_id" then fetch all soundtrack records A to Z.
Please somebody help me to show proper codeigniter query, how i can SELECT and JOIN Tables then fetch records from 3 tables, ?
Table 1 -> Category
cat_id
cat_name
- cat_title
date
Table 2 -> Album
- cat_id
album_id
album_title
album_details
Table 3 -> Soundtrack
album_id
track_title
- track_url
- date
Use this code in model
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屋!