 <p>I did it like</p> <pre><code>$where = new \Zend\Db\Sql\Where(); $where-&gt;equalTo('company_id',$company_id)-&gt;and -&gt;equalTo('users.status','1')-&gt;nest() -&gt;isNull('project_id')-&gt;or-&gt;equalTo('project_id',$project_id) -&gt;unnest(); $resultSet = $this-&gt;tableGateway-&gt;getSql()-&gt;select() -&gt;join(array('pu'=&gt;'project_users'), 'users.id = pu.user_id', array('project_id'), 'left') -&gt;join(array('r'=&gt;'roles'),'pu.role_id=r.id', array('role'),'left') -&gt;where($where); </code></pre>