简体   繁体   English

CodeIgniter Where_In从其他表中选择?

[英]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'); 我尝试了where_in方法,但是超时了,我尝试包括$this ->db_pfands -> where('crm_client_cheques.client id' ,'id');尝试$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. 原始查询应该返回703行,而当我删除被卡住的部分时,它会增加到3045,因此我需要将其包括在内。 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. 可能是您更改了几个名称,因为它们位于不同的数据库中,但是我相信您可以做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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