CLOSED:

Thank you Sagar. The Views UNION as described below was successful as built with the recommendations below. In the end with the real queries, it did not seem to be practical.

The better solution was to rewrite the Content Types so that they could both be manipulated through the same View query.

##########

I have two Views queries in hook_views_pre_execute that display correctly.
I am trying to display the superset of both queries using a UNION similar to:

function custom_views_pre_execute(&$view) {
  if ($view->name == "my_view") {
    $query1 = db_select('node', 'node');
    $query1->addField('node', 'title', 'node_title');
    $query1->addField('node', 'nid', 'nid');

    $query2 = db_select('node', 'node');
    $query2->addField('node', 'title', 'node_title');
    $query2->addField('node', 'nid', 'nid');

    $query=Database::getConnection()
      ->select($query1->union($query2))
      ->fields(NULL, array('node_title', 'nid'));

    $view->build_info['query'] = $query2;
    $view->build_info['count_query']=$view->build_info['query'];
  }
}

Unfortunately, the result is not displaying correctly.

I require help to correct this. I think the problem is either in the above hook or in the View itself.

Comments

Jaypan’s picture

What is your budget for this? Edit - as I mentioned in my response to your email, putting some indicator of this in your email will likely get you more responses. I don't actually use Views very much myself, so I'm not the guy for you in this case.

Sagar Ramgade’s picture

Hi,

In order to alter the views query, you need to use hook_views_query_alter, give it a try it should work.

http://api.drupalize.me/api/drupal/function/hook_views_query_alter/7

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

Bagz’s picture

To make this work you'll need to change the build_info query as well, like so:

$view->build_info['query'] = $query1->union($query2);

The counter will also be derived from that so any paging will work correctly.

TechNikh’s picture

You can use Views extra handlers module to union SQL queries of two different views/displays. https://www.drupal.org/project/views_extra_handlers
screenshot of union settings

Cheers,
TechNikh

In 30 seconds set up Automated Visual testing of your website. Zero coding. https://drupal.org/project/drulenium
Ever dreamed of styling your view, We have a solution for you. https://drupal.org/project/views_stylizer