简体   繁体   中英

CodeIgniter Where_In select from different table?

i am trying to covert this query in active record

SELECT 
crm_clients.id,
crm_clients.`moved_date`,
crm_clients.`contractor_id` 
FROM
dev_pfands.`crm_clients` 
WHERE crm_clients.`contractor_id` = 11 
AND (
crm_clients.`status` = 9 
OR crm_clients.`status` = 8 
OR crm_clients.`status` = 7
) 
AND crm_clients.id IN 
(SELECT 
 crm_client_cheques.`client_id` 
FROM
dev_pfands.`crm_client_cheques`) 
AND crm_clients.`moved_date` BETWEEN '2014-08-01' 
AND '2014-11-29 ' 
AND crm_clients.`contractor_id`<>''
GROUP BY crm_clients.`id

the section I'm having issue is

AND crm_clients.id IN 
    (SELECT 
     crm_client_cheques.client_id 
    FROM
    dev_pfands.crm_client_cheques) `

i've tried the where_in method but overtime i try to include my attempt of $this ->db_pfands -> where('crm_client_cheques.client id' ,'id'); get hit with errors and have no idea how to get past this.

the original query should return 703 rows and when I've removed the part I'm stuck with it increase to 3045 so i need it to be included. any help is appreciated.

First of all you have a error in your code.

$this->db_pfands->where('crm_client_cheques.client id', 'id');

This will be

$this->db_pfands->where('crm_client_cheques.client_id', 'id');

You have to provide the right column name and as far i know database's column name have not contain any space.

Now I think this active record query will help you to get your result.

$query = $this->db->select('crm_clients.id, crm_clients.moved_date, crm_clients.contractor_id')
                  ->where('moved_date BETWEEN "2014-08-01" AND "2014-11-29"')
                  ->where('contractor_id', 'id')
                  ->where_in('status', [9,8,7])
                  ->from('crm_clients')
                  ->join('crm_client_cheques', 'crm_client_cheques.client_id = crm_clients.id')
                  ->group_by('id')
                  ->get();
$result = $query->result();

May be you have change couple of names because they are in different database, but i believe you can do it.

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