Search code examples
phpmysqlcodeignitercodeigniter-3codeigniter-2

Complex query join using Codeigniter


I have a query which produce results in phpmyadmin but not in codeigniter.

$sql = "SELECT express_interests.*, 
       cl_to  .User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid

FROM express_interests  
    INNER JOIN users AS cl_to ON cl_to.User_Id = express_interests.To_Id
    INNER JOIN users AS cl_from ON cl_from.User_Id = express_interests.User_Id";

i want to use the same query in codeignitor. This is what i have used

$this->db->select('express_interests.*, 
       cl_to  .User_Name AS ToClient, 
       cl_from.User_Name AS FromClient,
       cl_from.Member_Id AS FromMid,
       cl_to.Member_Id AS ToMid
');

$this->db->from('express_interests');


$this->db->join('users AS cl_to', 'cl_to.User_Id = express_interests.To_Id');
$this->db->join('users AS cl_from', 'cl_from.User_Id = express_interests.User_Id');

when i use this it says

Unknown column 'cl_to .User_Name' in 'field list'

what is the proper way to use the above query in codeigniter.


Solution

  • $this->db->select('express_interests.*, 
           cl_to  .User_Name AS ToClient, 
           cl_from.User_Name AS FromClient,
           cl_from.Member_Id AS FromMid,
           cl_to.Member_Id AS ToMid
    ');
    

    Replace this with

    $this->db->select('express_interests.*, 
           cl_to.User_Name AS ToClient, 
           cl_from.User_Name AS FromClient,
           cl_from.Member_Id AS FromMid,
           cl_to.Member_Id AS ToMid
    ');
    

    Unwanted spacing in between table alias & column name