Drupal - Changing an inner join to a left join in views 3.3?

I found clicking on the reduce duplicates checkbox converted my INNER JOINs to LEFT JOINs:

depicting reduce duplicates tickbox

Before I was getting something akin to the following injected into my query:

INNER JOIN {field_data_field_random_field field_data_field_random_field} 
        ON node.nid = field_data_field_random_field.entity_id 
       AND (field_data_field_random_field.entity_type = 'node' 
       AND field_data_field_random_field.deleted = '0')

These inner joins were being caused by simple taxonomy filters, so sadly I didn't have any relationships to unrequire. Checking this box however doesn't seem to have altered my query in any other way, save for removing the inner joins, which is perfect for me; and may help someone else out there in this vast monkey adventure called life.


I used hook_views_query_alter() to change it. It takes a reference to the $view and $query objects. I checked the name of the view to see if it was the one I wanted, and then changed the join type in the query object for the table of interest:

function my_module_views_query_alter(&$view, &$query) {
  if($view->name == 'my_view') {
    $query->table_queue['field_data_field_my_field']['join']->type = "LEFT";
  }
}

When you go to Relationships tab in the views you will find a check box titled "Require this relationship".

enter image description here

  1. If you select this checkbox it means : Make an inner join
  2. If you uncheck this checkbox it means : Make a left join

Tags:

Database

Views

7