简体   繁体   English

如何在MySQL中联接多个表

[英]How to join multiple tables in MySQL

Here is my codes: 这是我的代码:

public function getallcontractfiles()
{
    $this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');         
    $this->db->from('contract_details as cd');
    $this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id','LEFT'); 

    if($this->session->userdata['user_type'] == 'ADMIN' && $this->session->userdata['user_group'] == '' ){
        $this->db->where('cd.Company_id',$this->session->userdata['company_id']);
        }
    if($this->session->userdata['user_type'] == 'USER'){
    $this->db->where('cd.users_id',$this->session->userdata['logged_user']);
    }
     $this->db->group_by('cd.Contract_Id '); 
    $this->db->order_by('cd.Contract_Id', 'desc');
     $query = $this->db->get();
    // print_r($this->db->last_query()); die;

        if ( $query->num_rows() > 0 )
        {
            foreach($query->result() as $row){
                $rows[] = $row;
            }
            return $rows;
        }
}

My question is: I have also a table called company and my App is all about to print out all contracts files with their companies. 我的问题是:我还有一个名为company的表,我的App即将打印出与其公司的所有合同文件。 I have defined Company_id as a foreign key in Contract_files. 我已经将Company_id定义为Contract_files中的外键。 Please help me to join company table to this function. 请帮助我加入公司表以执行此功能。 Thank you. 谢谢。

I'm not familiar with your ORM but this should make sense: 我不熟悉您的ORM,但这应该是有道理的:

$this->db->join('company', 'cf.Company_id = company.Company_Id','INNER');

Right after your join with contract_files table, Also specify fields you want to retrieve in the from company table as well. 在与contract_files表联接之后,立即在from公司表中也指定要检索的字段。

UPDATE 更新

Since your query is bringing up multiple contract files then you will have to do the same with company table as every contract file could have many companies, If they all have the same company, Then the foreign key Company_id should be in contract_details table. 由于您的查询显示了多个合同文件,因此您必须对company表执行相同的操作,因为每个合同文件可能有许多公司,如果它们都具有相同的公司,则外键Company_id应该位于contract_details表中。

IMPORTANT 重要

GROUP_CONCAT() has a limitation of 1024 by default, So it's not the best idea to put files names in it. GROUP_CONCAT()默认情况下限制为1024,因此将文件名放在其中并不是最好的主意。

public function getallcontractfiles(){
$this->db->select('cd.*, GROUP_CONCAT(cf.Contract_File_Name) AS fileslink');         
$this->db->from('contract_details as cd');
$this->db->join('contract_files as cf', 'cd.Contract_Id = cf.Contract_Id'); 

if($this->session->userdata('user_type') == 'ADMIN' && $this->session->userdata('user_group') == '' ){
    $this->db->where('cd.Company_id',$this->session->userdata('company_id'));
    }
if($this->session->userdata('user_type') == 'USER'){
$this->db->where('cd.users_id',$this->session->userdata('logged_user'));
}
 $this->db->group_by('cd.Contract_Id '); 
$this->db->order_by('cd.Contract_Id', 'desc');
 $query = $this->db->get();
// print_r($this->db->last_query()); die;

    if ( $query->num_rows() > 0 )
    {
        foreach($query->result() as $row){
            $rows[] = $row;
        }
        return $rows;
    }
}

Try This Code. 尝试此代码。

LEFT JOIN table2 ON table1.column_name = table2.column_name; 左联接table2 ON table1.column_name = table2.column_name;

RIGHT JOIN table2 ON table1.column_name = table2.column_name; 右联接table2 ON table1.column_name = table2.column_name;

INNER JOIN table2 ON table1.column_name = table2.column_name; INNER JOIN table2 ON table1.column_name = table2.column_name;

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

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