我在codeigniter中连接两个表时遇到此错误。
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 '.`id = a`.`assignee_key where a`.`assigned_to="office" and
user`.`id="53"`' at line 1
select a`.* from address as `a LEFT JOIN user ON user`.`id = a`.`assignee_key where a`.`assigned_to="office" and user`.`id="53"
$this->db->select('select a.* from address as a LEFT JOIN user ON user.id = a.assignee_key where a.assigned_to="office" and user.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();
答案 0 :(得分:0)
别名表名称有问题。试试这个
$this->db->select('select a.* from address a LEFT JOIN user u ON u.id = a.assignee_key where a.assigned_to="office" and u.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();
答案 1 :(得分:0)
你有两个选择。以下是查询的更好/结构化版本。
$this->db->select("a.*")
->from("address as a")
->join("user","user.id = a.assignee_key","left")
->where(array("a.assigned_to"=>"office","user.id"=>$user_id));
$query = $this->db->get();
return $query->row();
更新:对于评论,请尝试左外连接
->join("user","user.id = a.assignee_key","left outer")
它保留第一个(左)表中不匹配的行,将它们与第二个(右)表形状的NULL行连接。