I need union to merge several queries in views, but I cannot find anything inside views that references union. Is this not possible?

Here is an example that I'd like to migrate to a view:

  $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));

  // Search for broken links in nodes and comments and blocks of all users.
  // @todo Try to make UNION'ed subselect resultset smaller.
  $subquery4 = db_select('linkchecker_node', 'ln')
    ->distinct()
    ->fields('ln', array('lid'));

  $subquery3 = db_select('linkchecker_comment', 'lc')
    ->distinct()
    ->fields('lc', array('lid'));

  $subquery2 = db_select('linkchecker_block_custom', 'lb')
    ->distinct()
    ->fields('lb', array('lid'));

  // UNION all linkchecker type tables.
  $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', array('lid'));

  // Build pager query.
  $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
  $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
  $query->fields('ll');
  $query->condition('ll.last_checked', 0, '<>');
  $query->condition('ll.status', 1);
  $query->condition('ll.code', $ignore_response_codes, 'NOT IN');

If UNION is really missing, this need to be added to views. It's really common SQL.

Comments

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Views has no support for UNION.

The requirements for UNION do not really work well with the basic structure of Views, honestly. Getting fields to match up across subqueries in a union is very, very difficult when you approach it from the UI perspective.

hass’s picture

Category: feature » support
Priority: Major » Normal
Status: Closed (won't fix) » Active

This are very bad news. Is it possible to join a custom query that is prepared outside of the views UI? So that the $subquery1 can be joined from the configurable view?

I'm guess I'm able to build this in the views UI:

// Build pager query.
  $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
  $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
  $query->fields('ll');
  $query->condition('ll.last_checked', 0, '<>');
  $query->condition('ll.status', 1);
  $query->condition('ll.code', $ignore_response_codes, 'NOT IN');

and than I need a relationship that may has static queries that cannot configured via the views UI:

  $subquery4 = db_select('linkchecker_node', 'ln')
    ->distinct()
    ->fields('ln', array('lid'));

  $subquery3 = db_select('linkchecker_comment', 'lc')
    ->distinct()
    ->fields('lc', array('lid'));

  $subquery2 = db_select('linkchecker_block_custom', 'lb')
    ->distinct()
    ->fields('lb', array('lid'));

  // UNION all linkchecker type tables.
  $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', array('lid'));
hass’s picture

Status: Active » Fixed

If someone else looking for a workaround, see http://drupal.org/node/965720#comment-6876290.

hass’s picture

Category: support » feature
Status: Fixed » Closed (won't fix)