简体   繁体   English

如何在我在查询中有查询的条件下返回结果

[英]How to return result in condition that i have a query inside query

I have write a query inside query where i want to return the result when the fname (which is the field of table1) is not equal to name(which is the field of table2).我在查询中写了一个查询,当 fname(它是 table1 的字段)不等于 name(它是 table2 的字段)时,我想在其中返回结果。 but when i compile this query i get the fatal error.Please check the line of code below.And suggest me where i am wrong.但是当我编译这个查询时,我得到了致命错误。请检查下面的代码行。并告诉我我错在哪里。

table1: is equal to ult_camp表1:等于ult_camp

table2: is equal to ult_coach table2: 等于 ult_coach

$rs = $this->db->select('ult_camp.*,ult_country.name as country_name,ult_state.name as state_name')
                    ->join('ult_country', 'ult_country.id = ult_camp.country_id')
                    ->join('ult_state', 'ult_state.id = ult_camp.state_id ')
                    ->from('ult_camp')
                    ->where("'org_fname' NOT IN (SELECT 'name' FROM 'ult_coach')", NULL, FALSE)
                    ->where('ult_camp.status', 'Active')
                    ->where('ult_state.status', 'Active')
                    ->where('ult_country.status', 'Active')
                    ->get()->result(); 

Please help me to get this issue.Any help will be appreciated.请帮我解决这个问题。任何帮助将不胜感激。

Thanks in advance.提前致谢。

Your first WHERE function call is using raw SQL, so what you currently have won't work.您的第一个WHERE函数调用使用的是原始 SQL,因此您目前所拥有的将无法正常工作。 You could try using whereRaw instead:您可以尝试使用whereRaw代替:

->whereRaw("org_fname NOT IN (SELECT name FROM ult_coach)")
->where('ult_camp.status', 'Active')
...

But you could also phrase your query as a left join to the ult_coach table:但是您也可以将您的查询ult_coach为对ult_coach表的左连接:

$rs = $this->db->select('ult_camp.*,ult_country.name as country_name,ult_state.name as state_name')
                ->join('ult_country', 'ult_country.id = ult_camp.country_id')
                ->join('ult_state', 'ult_state.id = ult_camp.state_id ')
                ->leftJoin('ult_coach', 'ult_camp.org_fname = ult_coach.name')
                ->from('ult_camp')
                ->whereNull('ult_coach.name')
                ->where('ult_camp.status', 'Active')
                ->where('ult_state.status', 'Active')
                ->where('ult_country.status', 'Active')
                ->get()->result(); 

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

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