Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedViews 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.
Comment #2
hass CreditAttribution: hass commentedThis 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:
and than I need a relationship that may has static queries that cannot configured via the views UI:
Comment #3
hass CreditAttribution: hass commentedIf someone else looking for a workaround, see http://drupal.org/node/965720#comment-6876290.
Comment #4
hass CreditAttribution: hass commented