Add a NOT IN condition and a nested query to your views WHERE condition

PROBLEM: 

I needed to add a NOT IN condition with a nested query for the WHERE condition. The usual method wont work

 

$view->query->where[1]['conditions'][] = array(
            ['field'] => 'groups_group_membership.type'
            ['value'] => array
                (
                    [0] => 'SELECT_QUERY_HERE'
                  
                )

            ['operator'] => IN
        );

SOLUTION :

The way that works

 

/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query
 */
function your_module_views_query_alter(&$view, &$query) {
  if ($view->name == 'your_view' && $view->current_display == 'your_display') {

    $view->query->where[1]['conditions'][] = array(
      'field' => 'your_field' NOT IN (SELECT gm.uid from group_membership gm where gm.gid=groups_group_membership_parent_groups.gid)',
      'value' => array(),
      'operator' => 'formula'
    );
    
  }
}