加入三桌

时间:2015-11-04 07:54:12

标签: mysql sql codeigniter

请帮助我

我有三个表,其中名称是公司,管理员和潜在客户。我从公司= user_name获取所有潜在客户数据和一条记录,从admin = display_name获取一条记录但在这里我很困惑Coz公司和管理员' ID'保存在相同的领域..请任何人帮助我..谢谢

从评论中添加:

我的查询

SELECT inno_admin.display_name,inno_company.Pseudo_name,inno_leads.* FROM inno_leads INNER JOIN inno_company ON inno_company.id = inno_leads.agent_id inno_admin ON inno_admin.id=inno_leads.agent_id ORDER BY ID DESC

2 个答案:

答案 0 :(得分:0)

试试这段代码。

只需在条件中加2即可。

RecyclerView

答案 1 :(得分:0)

不确定这对你的问题有点困惑。下面的代码可以让您获取所有数据。

http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

public function get_data_results() {
    $this->db->select('*');
    $this->db->from('inno_leads', 'LEFT');
    $this->db->join('inno_company', 'inno_company.id = inno_leads.agent_id', 'LEFT');
    $this->db->join('inno_admin', 'inno_admin.id = inno_leads.agent_id', 'LEFT');
    // order_by id up to you but there
    $this->db->order_by('inno_leads.agent_id', 'DESC');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        //return $query->result();
        //Or
        //return $query->result_array();
    } else {
        return FALSE;
    }

}

将$ name从控制器传递到模型。

public function get_data($name) {
    $this->db->select('*');
    $this->db->from('inno_leads', 'LEFT');
    $this->db->join('inno_company', 'inno_company.id = inno_leads.agent_id', 'LEFT');
    $this->db->join('inno_admin', 'inno_admin.id = inno_leads.agent_id', 'LEFT');
    $this->db->where("inno_company.company_name", $name);
    $this->db->where("inno_admin.display_name", $name);
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->row_array();
    } else {
        return FALSE;
    }

}

控制器示例功能

public function index() {
    $this->load->model('some_model');

   // Note this could be a uri segment also or session data.
   // $name = $this->uri->segment(2); 

    $name = 'Your Company Name'; 

    $company_info = $this->some_model->get_data($name);

    if ($company_info) {

        $data['display_name'] = $company_info['display_name'];

        $this->load->view('home', $data);
    }
}