2016-03-07 122 views
0

我在codeigniter中加入兩個表時遇到此錯誤。錯誤號:1064

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

我猜想使用'AS'沒有什麼錯誤。 –

+0

似乎你缺少左'周圍表名.. \'表名\'\'列名\' – diEcho

回答

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(); 
+0

謝謝,但還是我得到的錯誤:錯誤編號:1064 您的SQL語法錯誤。檢查對應於您MariaDB的服務器版本使用附近的正確語法手冊「.'assignee_key其中'a'.assigned_to =‘辦公室’和'u'.'id =‘53’」在1號線 SELECT'選擇a'.' *從地址左連接用戶u ON u'.'id = a'.'assignee_key其中a'.assigned_to = 「辦公室」 和'u'.'id = 「53」' –

+0

@Sunilkumarm是那assignee_key或assigned_key? – Yoshioka

+0

完全assignee_key。 –

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") 

它保留了不匹配的行從第一個(左)表中,第二個形狀以空行加入他們(右)表。

+0

謝謝,沒有錯誤。但我想顯示的結果如果與user_ID的地址表中沒有數據,也就是說,如果我沒有在地址表中的user_id記錄我需要顯示空記錄,但我越來越嚴重:注意 消息:試圖讓非財產-object –

+0

嘗試左外側代替左側。 –

+0

我已經嘗試了左外部,但得到相同的錯誤。 –