选择数据库时使用AS的codeigniter

时间:2013-06-06 04:40:42

标签: codeigniter

我无法找到关于JOIN AS的用户指南的任何信息,这是我正在尝试做的事情:

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声明中似乎无法找到的指南中的部分

3 个答案:

答案 0 :(得分: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中提及连接表名称,如果您在表名后面指定名称,它将自动为您要加入的表别名。

尝试使用Ci

$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');       

答案 1 :(得分: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');

答案 2 :(得分:0)

$this->db->select('t1.*, t2.*')
         ->join('table2 AS t2', 't1.column = t2.column', 'left');

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