更改mysql,其中查询到codeigniter活动记录

时间:2011-01-04 22:25:44

标签: php mysql codeigniter activerecord

是否可以纠正此查询

`$sql = '
    SELECT *
    FROM employers
    LEFT JOIN jobwall ON jobwall.employers_employer_id = employers.employer_id
    WHERE employers.employer_id
    IN (SELECT employers_employer_id FROM jobwall)
';

$this->db->query($sql);`

作为codeigniter中的活动记录

2 个答案:

答案 0 :(得分:2)

我同意Joe Stefanelli,LEFT JOIN和WHERE IN子查询对于普通连接是不必要的。以下应该做你想要的:

$this->db->join('jobwall'
    , 'jobwall.employers_employer_id = employers.employer_id');
$this->db->get('employers');

只有当你想让所有雇主都知道他们是否在工作区表中时,你才会使用LEFT JOIN。

答案 1 :(得分:1)

我正在寻找相同的东西。然后得到了适当的答案。

$this->db->select('employers_employer_id');
$this->db->from('jobwall');

$subquery = $this->db->_compile_select();
$this->db->_reset_select();
$subquery = str_replace("\n"," ",$subquery);

$this->db->where_in('tag_page_id', $subquery, false); //false=if no escape for subquery 
$this->db->join('jobwall', 'jobwall.employers_employer_id = employers.employer_id','left');
$this->db->get('employers');