繁体   English   中英

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

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

我在查询中写了一个查询,当 fname(它是 table1 的字段)不等于 name(它是 table2 的字段)时,我想在其中返回结果。 但是当我编译这个查询时,我得到了致命错误。请检查下面的代码行。并告诉我我错在哪里。

表1:等于ult_camp

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(); 

请帮我解决这个问题。任何帮助将不胜感激。

提前致谢。

您的第一个WHERE函数调用使用的是原始 SQL,因此您目前所拥有的将无法正常工作。 您可以尝试使用whereRaw代替:

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

但是您也可以将您的查询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