北漂IT民工 的博客

Code igniter 多表查询的实现

网上关于Code Igniter的多表查询的例子比较少。

大家大部分使用的是join。


但是这不表示ci不能多表查询。


下面给一个ci的多表查询的示例代码,摘自一个函数

希望能对新人会有帮助。



$this->db->select(‘

bu_g_l.alias as name,

bu_g_l.info as intro,

bu_g_l.code,

‘);


$this->db->from(

$this->_table_name . ‘ as u_t_g_l,’ .

$this->_table_name_user_tag . ‘ as u_t,’ .

$this->_table_name_business_unit_goods . ‘ as bu_g,’ .

$this->_table_name_business_unit_goods_list . ‘ as bu_g_l,’ .

);


$this->db->where(‘u_t.user_id’, $user_id);

$this->db->where(‘u_t.tag_id’, $tag_id);

$this->db->where(‘(u_t_g_l.user_tag_id = u_t.user_tag_id)’);

$this->db->where(‘(u_t_g_l.business_unit_goods_id = bu_g.id)’);

$this->db->where(‘(bu.id = bu_g.business_unit_id)’);


if($limit) {

$this->db->limit($limit, $offset);

}


$query = $this->db->get();

$error = $this->db->_error_message();

if ($error)

return -1;

return $query->result_array();