Multiple Joins in Codeigniter

前端 未结 4 1947
孤独总比滥情好
孤独总比滥情好 2020-12-08 23:33

I\'m new to building databases and I\'m trying to do a JOIN based on a having three database tables.

Table A = ID, Name, etc
Table B = ID, Name, etc
Table C          


        
4条回答
  •  春和景丽
    2020-12-09 00:07

    $this->db->select('*');
    $this->db->from('TableA AS A');// I use aliasing make joins easier
    $this->db->join('TableC AS C', 'A.ID = C.TableAId', 'INNER');
    $this->db->join('TableB AS B', 'B.ID = C.TableBId', 'INNER');
    $result = $this->db->get();
    

    The join function works like this: join('TableName', 'ON condition', 'Type of join');

    The equivilent sql:

    SELECT *
    FROM TableA AS A
        INNER JOIN TableC AS C
        ON C.TableAId = A.ID
        INNER JOIN TableB AS B
        ON B.ID = C.ID
    

    I found that writing the SQL first, testing it, then converting to the active record style minimizes error.

提交回复
热议问题