2013-06-06 25 views
1

我无法找到用户指南做JOIN任何信息在使用AS,这里是我尝试做的事:codeignitor选择DATABSE

SELECT 
c.id 
,c.name AS companyName 
,con.id AS conID 
,l.id AS lid 

FROM 
    company AS c 
LEFT JOIN 
    contacts AS con ON 
    c.primary_contact = con.id 
LEFT JOIN 
    locations AS l ON 
    c.id = l.cid 

人有一个快速的解决方案,或我指向在引导部分,我似乎无法找到与AS语句

回答

0

尝试这样

SELECT 
    c.id 
    ,c.name AS companyName 
    ,con.id AS conID 
    ,l.id AS lid 

FROM 
    company c 
LEFT JOIN 
    contacts con ON 
    c.primary_contact = con.id 
LEFT JOIN 
    locations l ON 
    c.id = l.cid 

你不必提也加入表名与AS,如果你在表名后给其命名会AUTOMATICA lly别名你的桌子,你加入。

尝试词像

$this->db->select('c.id 
        ,c.name AS companyName 
        ,con.id AS conID 
        ,l.id AS lid'); 
$this->db->from('company c');   
$this->db->join('contacts con','c.primary_contact = con.id','left'); 
$this->db->join('locations l','c.id = l.cid','left');  
+0

IM不知道我跟着你将如何添加模型页。这里是我的模型页面的样子: ' $ this-> db-> select('c.id'); $ this-> db-> from('company AS c'); $ this-> db-> join('contacts','contacts.id = company.primary_contact'); $ query = $ this-> db-> get(); return $ query-> result_array(); ' – bnelsonjax

+0

抱歉在我的评论中混乱的代码,显然你不能添加代码的评论,似乎很愚蠢。 – bnelsonjax

+0

看我的编辑.... – Gautam3164

0

林不知道这个,但你可以试试这个:

$this->db->select('company.id','name','contacts.id','locations.id'); 
$this->db->from('company'); 
$this->db->join('contacts','company.primary_contact = contacts.id','left'); 
$this->db->join('locations','contacts.id = locations.id','left'); 
0
$this->db->select('t1.*, t2.*') 
     ->join('table2 AS t2', 't1.column = t2.column', 'left'); 

return $this->db->get('table1 AS t1')->result();