如何使用PHP和MySQL以及AND,OR和NOT运算符过滤交叉表

I have a table of customers:

customers
id name
1  test
2  test2

And a table of groups:

groups
id name
1  g1
2  g2
3  g3
4  g4

And a cross table:

rs_customers_groups
customerid groupid
1          1
1          3
2          1
2          4

That's all i currently have for testing.

What I want to do is to filter my cross table using a HTML-Formular that looks like that where I can say AND, OR and NOT:

form

My PHP looks like that and creates the following query:

<?php
     if(isset($_POST["build"])){
        $query = 'SELECT * FROM rs_customers_groups WHERE 1 AND ';
        foreach ($_POST as $key => $value) {
             if($key != 'build'){
                  echo $key." ".$value."<br />";
                  if($value == 0){
                      $operator = "OR";
                      //$query.='('.$operator.' gruppe = '.$key.')';
                  }else if($value == 1){
                        $operator == "OR";
                  } else if($value == 2){
                        $operator == "NOT";
                  }
            }
        }
         echo $query.'<br />';
     }
?>

SELECT * FROM rs_customers_group WHERE 1 AND groupid = 1 AND groupid = 2

Alright, that's what I have done already. What I want to do is creating a filter.

For example:

When my form is like g1 AND g2 NOT g3 AND g4 NOT

I want all customers that are in group 1 AND 3 but NOT in group 2 AND 4. The result should be customer 1.

When my form is like this: g1 AND g2 NOT g3 NOT g4 NOT

I want all customers that are in group 1 but NOT in 2,3 and 4

Would there be AND and NOT enought and I do not need the OR option?

thanks!