简体   繁体   English

Codeigniter 2.1-MySQL连接

[英]Codeigniter 2.1 - MySQL JOIN

I have two tables: 我有两个表:

MENU
->id_menu
->title
->page_id
->order

PAGES
->id_page
->title
->page
->slug

This is select function: 这是选择功能:

public function get_all_menu()
    {
        return $this->db
        ->select('menu.*, pages.id_page, pages.title AS page_title')
        ->from($this->table)
        ->join('pages','id_page = page_id')
        ->order_by($this->order_by)
        ->get()
        ->result_array();
    }

Here is my problem - item in the menu can be connected with page, but also it can be solo (without connection to page). 这是我的问题-菜单中的项目可以与页面连接,但也可以是单独的(不与页面连接)。 This means page_id in MENU table can be 0. If page_id is 0, I am not getting that row from above query. 这意味着MENU表中的page_id可以为0。如果page_id为0,则无法从上面的查询中获取该行。 How can I get all items in the menu (those which are connected and those which are not connected with page )? 如何获得菜单中的所有项目(已连接的项目和未与page连接的项目)?

You need add the join type in ->join() 您需要在->join()添加join type

like 喜欢

public function get_all_menu()
 {
    return $this->db
    ->select('menu.*, pages.id_page, pages.title AS page_title')
    ->from($this->table)
    ->join('pages','id_page = page_id','left')
    ->order_by($this->order_by)
    ->get()
    ->result_array();
}

See reference manual Active Record 请参阅参考手册Active Record

As requested from comments: 根据评论的要求:

You need to use "LEFT JOIN" to retrieve partial or incomplete values from the tables. 您需要使用“ LEFT JOIN”从表中检索部分或不完整的值。

In this case, the regular join is looking the matches between the two tables. 在这种情况下,常规联接将查找两个表之间的匹配项。 Since there's no id 0 in one of the tables, the match can't be retrieved and the row won't be selected. 由于其中一张表中没有id 0,因此无法检索到匹配项,也不会选择该行。

Using a LEFT JOIN (or RIGHT JOIN when appropriate, based on which one is the "incomplete" table) if the match isn't retrieved, the outcome will contain also the rows which aren't matched, with all NULLs in the other table's values 如果未检索到匹配项,则使用LEFT JOIN(或适当时使用RIGHT JOIN,基于哪个是“不完整”表),结果还将包含不匹配的行,另一表的所有NULL值

From Your Controller call this method in Model Say your Model Name is general_model 从您的控制器在模型中调用此方法说您的模型名称为general_model

 $value=('menu.*, pages.id_page, pages.title AS page_title'); $joins = array ( array ( 'table' => 'pages', 'condition' => 'menu.id_page = pages.page_id', 'jointype' => 'leftouter' ), ); $where_condition=array('menu.Id '=>'Enable','restaurant.Id'=>$rest_id); $data['detail'] = $this->general_model->get_joins('menu',$value,$joins,$where_condition,'menu.Id','asc')->result(); 

here $where_condition is a where condition you want to pass if u want to remove you can remove it from function.,And Last two parameters are orderby 这里$ where_condition是要传递的where条件,如果要删除,可以将其从函数中删除。最后两个参数是orderby

Simply use this function in your Model For Join 只需在要加入的模型中使用此功能

 public function get_joins($table,$value,$joins,$where,$order_by,$order) { $this->db->select($value); if (is_array($joins) && count($joins) > 0) { foreach($joins as $k => $v) { $this->db->join($v['table'], $v['condition'], $v['jointype']); } } $this->db->order_by($order_by,$order); $this->db->where($where); return $this->db->get($table); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM