$this->db->select('p.clientID as pClientID, m.clientID as mClientID'); $this->db->distinct(); $this->db->from('Hours AS h'); $this->db->join('Jobs j', 'j.jobID = h.jobID', 'left'); $this->db->join('Projects p', 'j.projectID = p.projectID', 'left'); $this->db->join('Maintenance m', 'j.maintenanceID = m.maintenanceID', 'left'); $this->db->where('h.invoiceID', 0); $this->db->where('( p.clientID IS NOT NULL or m.clientID IS NOT NULL)'); $this->db->where('h.hourID IS NOT NULL');