user6891871
user6891871

Reputation: 174

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). 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

table2: is equal to 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.

Upvotes: 1

Views: 50

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521674

Your first WHERE function call is using raw SQL, so what you currently have won't work. You could try using whereRaw instead:

->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:

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

Upvotes: 1

Related Questions