2017-03-01 67 views
0

如何在Codeigniter框架中運行此sql查詢?使用Codeigniter中的嵌套連接

SELECT users.*, 
    GROUP_CONCAT(category.title SEPARATOR ',') as title 
    FROM users 
    LEFT JOIN (
    procducts as pr 
    INNER JOIN category ON pr.category_id =category.id 
) 
    ON (users.id=pr.user_id) 
GROUP BY users.id 

在笨的Active Record(查詢生成器類),而不使用db->query

+1

[codeigniter表連接]的可能重複(http://stackoverflow.com/questions/6024800/codeigniter-table-join) –

+0

@ hamza-zafee我在查詢問題之前看到此鏈接 – user3046493

回答

0

的笨文檔explain a join喜歡如下:

join($table, $cond[, $type = ''[, $escape = NULL]]) 

沒有文檔上嵌套連接的支持。所以,你可以創建一個查詢,如:

$this->db->select('users.*, GROUP_CONCAT(category.title SEPARATOR ',') as title') 
     ->from('users') 
     ->join('procducts as pr INNER JOIN category ON pr.category_id =category.id','users.id=pr.user_id','left') 
     ->group_by('users.id'); 
$query = $this->db->get(); 
+0

它有一點問題''''之前和affter pr.procducts – user3046493

+0

請解釋你的問題,什麼是生成的查詢?你可以在查詢這行後使用echo $ this-> db-> last_query();死;' – Vickel

0

我用這個代碼

$this->db->select(" 
       users.*,GROUP_CONCAT(category.title SEPARATOR ',')    
       as title FROM users LEFT JOIN 
         (procducts as pr INNER JOIN category ON       
          pr.category_id =category.id) 
         ON (users.id=pr.user_id)"); 


$this->db->group_by("users.id"); 

這個代碼它的確定,並沒有任何問題。