Drupal views custom filter, check if row exist
I guess you are looking for something like this, but you might need to tweak it a bit. You can group db_or and db_and, then combine them like this. Basically create two 'and' groups where all conditions need to match. Then combine those in an 'or' group, where one of the conditions needs to match.

function query() {
  $and1 = db_and();
    $and1-&gt;condition('field_name', 'test', 'LIKE');
    $and1-&gt;condition('field_id', 0);

  $and2 = db_and();
    $and2-&gt;condition('field_name', 'test', '&lt;&gt;');

  $combine = db_or();
    $combine-&gt;condition($and1);
    $combine-&gt;condition($and2);

  $this-&gt;query-&gt;add_where(
    $this-&gt;options['group'],
    $combine
  );
}