Using db_select with subqueries
First of all, you can prepare a SelectQuery object using db_select(), then use it in place of a table in another db_select(). Example (off-the-cuff, not tested):

$inner_query = db_select('comment', 'c')
  -&gt;fields('c');
$outer_query = db_select($inner_query, 'sub');


Second of all, you can UNION two prepared SelectQuery objects with the SelectQuery::union() method.

$some_query = db_select('comment_table', c)-&gt;fields('c');
$union_query = db_select('other_comment_table', o)-&gt;fields('o')-&gt;union($some_query, 'ALL');


So all the tools you need are there; it'll just take some trial and error to put them all together.