Search code examples
mysqlcodeigniterleft-join

LEFT JOIN in Codeigniter shows syntax error


I want to select a table which holds all the issues a specific project id has. For that I am planning to do a LEFT JOIN and get the projects_id in Projects table to the column 'issues_projects_id' in issues table. Isn't it a JOIN that has to be used here?

    // $this->db->join('issues', 'issues.issues_projects_id = projects.projects_id', 'left');
   // $query1 = $this->db->get();
   $squery="SELECT * FROM projects LEFT JOIN issues ON issues.issues_projects_id=projects.projects_id";
    $query1= $this->db->query($squery);
    $query= $this->db->query($squery);
    return $query->result();
    return $query1->result();

The above code shows below error & displays no output.

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LEFT JOIN `issues` ON `issues`.`issues_projects_id` = `projects`.`projects_id`' at line 2

SELECT * LEFT JOIN `issues` ON `issues`.`issues_projects_id` = `projects`.`projects_id`

Filename: C:/wamp64/www/projects/issue_tracker/system/database/DB_driver.php

Line Number: 691

Projects Table

projects_id projects_name
1 abc
2 xyz

Issues Table

issues_id issues_description issues_projects_id
1 gfj 1
2 fghgj 1

Solution

  • you are missing from statement:

    SELECT * 
    FROM `projects` --< here 
    LEFT JOIN `issues` 
      ON `issues`.`issues_projects_id` = `projects`.`projects_id`
    WHERE projects_id = ?