2012-08-31 49 views
0

> * 1.我需要在活動記錄寫這篇*笨的活動記錄複雜的查詢

我需要加入這些3個表,但加入的條件是非常非常有選擇性

$this->db->select(name); 
$this->db->from('table0'); 
$this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1) OR (table1.id=1 AND table0.feild2 = table1.feild2)') // <--- how to write this is my question 

我可以做一個簡單的連接,但主要的問題是實現上面提到的連接的條件。這也是非常,非常非常小的一部分!大的查詢,所以我真的不能改變它回到原生SQL查詢,如:

$this->db->query('//entire sql query'); //cant do this, need to write ACTIVE RECORDS 

當我寫的活動記錄,螢火引發我一個錯誤說:

你在你的SQL有一個錯誤句法;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 接近「)或

有什麼建議?

回答

0

說實話,我不知道這是可能的。 CI不使用真正的活動記錄,因此每種方法都有非常嚴格的參數,而且我沒有看到任何可以被誘騙執行所需任務的東西。

我會嘗試重新編寫查詢了一下:

$this->db->select(name); 
$this->db->from('table0'); 
$this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1)', LEFT); 
$this->db->join('table1','(table1.id=1 AND table0.feild2 = table1.feild2)', LEFT); 
$this->db->where('(table1.id=0 AND table0.feild1 = table1.feild1)'); 
$this->db->or_where('(table1.id=1 AND table0.feild2 = table1.feild2)'); 

我相信這個查詢將返回正確的價值觀,但你肯定會想對其進行全面測試。希望這至少能讓你朝着正確的方向前進。

+0

整齊!謝謝你的幫助 –