Question: I am trying to run following query in CodeIgniter but it is not returning the correct output
According to the query, one of the condition is that if the value of $supplier\_id is not matched then no result should be displayed. However, it is displaying all the result no matter what the value of supplier\_id is. Can anyone please help me in correcting this query

```
public function get_onboarded_detail()
    {
        $supplier_id = 40;
        $query = $this->db->select('*')

          ->from('job')
          ->join('job_workforce', 'job_workforce.job_id = job.id', 'Right')
          ->where('job_workforce.supplier_id',$supplier_id)
          ->where('job_workforce.status','onboarded')
          ->or_where('job_workforce.status','job_offer')
          ->or_where('job_workforce.status','offer_accepted_by_client')
          ->or_where('job_workforce.status','offer_accepted_by_supplier')
          ->get();
          $res =  $query->result();          
    }

```

Answer: 
The actual problem with your where condition because its contain "OR" condition, so whatever your supplier\_id system will return true result. see below example:-

Your current where condition:-

```
WHERE job_workforce.supplier_id = 40 AND job_workforce.status = 'onboarded' OR job_workforce.status = 'job_offer' OR job_workforce.status = 'offer_accepted_by_client' OR job_workforce.status = 'offer_accepted_by_supplier' 

```

It should be:-

```
WHERE job_workforce.supplier_id = 40 AND (job_workforce.status = 'onboarded' OR job_workforce.status = 'job_offer' OR job_workforce.status = 'offer_accepted_by_client' OR job_workforce.status = 'offer_accepted_by_supplier') 

```

To make a correction using codeigniter please use:

```
public function get_onboarded_detail()
{
    $supplier_id = 40;
    $query = $this->db->select('*')

      ->from('job')
      ->join('job_workforce', 'job_workforce.job_id = job.id', 'Right')
      ->where("job_workforce.supplier_id = $supplier_id AND (job_workforce.status = 'onboarded' OR job_workforce.status = 'job_offer' OR job_workforce.status = 'offer_accepted_by_client' OR job_workforce.status = 'offer_accepted_by_supplier')");

      ->get();
      $res =  $query->result();          
}

```
This is how you can use the codeigniter grouping of where conditions. You can make groups accordingly.

```
public function get_onboarded_detail() {

    $supplier_id = 40;
    $query = $this->db->select('*')
                  ->from('job')
                  ->join('job_workforce', 'job_workforce.job_id = job.id', 'Right')
                  ->group_start()
                      ->where('job_workforce.supplier_id',$supplier_id)
                      ->where('job_workforce.status','onboarded')
                  ->group_end()
                  ->group_start()
                      ->or_where('job_workforce.status','job_offer')
                      ->or_where('job_workforce.status','offer_accepted_by_client')
                      ->or_where('job_workforce.status','offer_accepted_by_supplier')
                  ->group_end()
                  ->get();
         $res =  $query->result();          
}

```