简体   繁体   中英

change mysql where_in query to codeigniters active record

is it possible to right this query

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

as active-record in codeigniter

I agree with Joe Stefanelli, the LEFT JOIN and the WHERE IN subquery are unnecessary with a normal join. The following should do what you want:

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

You would use a LEFT JOIN only if you wanted to get all employers whether or not they were in the jobwall table.

I was searching for same. Then got appropriate answer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM