Drupal views custom filter, check if row exist I need help with a query. I am having a really hard time going in the right direction.

I have a custom multi-value field, containing one 'name' and one 'id'.
I want to filter on contents that: 


None of the delta has a name 'test' ('test' is a name example).
OR, the name is 'test' and the id is equal to 0.


I was planning on having the logic in the customer filter, like so:

class my_module_handler_filter_field_name extends views_handler_filter_boolean_operator {
  function query() {
      // My custom code here
  }
}


But I don't have much success for now. I managed to filter rows where 'name' = 'test' using:

$this-&gt;query-&gt;add_where($this-&gt;options['group'], 'field_name', 'test', 'LIKE');


But if I do the opposite with 'NOT LIKE', it just shows me every row not equal to 'test' (also including rows from an item that has one name = 'test'). This behaviour is normal, I know it is simply not the right way.

A table example:


    entity_id    delta    store_name  id_in_store
    ---------------------------------------------
    1            0        Amazon      0
    1            1        BestBuy     123442111
    2            0        Apple       123442123
    2            1        Microsoft   124423459
    2            2        Google      112559002


Entity_id is a node ID for a 'product' content type. I want to be able to know which products are sold at Amazon. And there are 2 ways to know it:


The field have a delta where store_name is 'Amazon', but id = 0.
Or the the field doesn't have any delta where store name is 'Amazon'.
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
  );
}