diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php
index 22aca8f..f8cbbfb 100644
--- a/core/lib/Drupal/Core/Database/Query/Select.php
+++ b/core/lib/Drupal/Core/Database/Query/Select.php
@@ -606,8 +606,9 @@ class Select extends Query implements SelectInterface {
 
     $group_by = $count->getGroupBy();
     $having = $count->havingConditions();
+    $unions = $count->getUnion();
 
-    if (!$count->distinct && !isset($having[0])) {
+    if (empty($unions) && !$count->distinct && !isset($having[0])) {
       // When not executing a distinct query, we can zero-out existing fields
       // and expressions that are not used by a GROUP BY or HAVING. Fields
       // listed in a GROUP BY or HAVING clause need to be present in the
@@ -635,7 +636,9 @@ class Select extends Query implements SelectInterface {
 
     // If we've just removed all fields from the query, make sure there is at
     // least one so that the query still runs.
-    $count->addExpression('1');
+    if (empty($unions)) {
+      $count->addExpression('1');
+    }
 
     // Ordering a count query is a waste of cycles, and breaks on some
     // databases anyway.
@@ -648,12 +651,6 @@ class Select extends Query implements SelectInterface {
       $count->distinct = FALSE;
     }
 
-    // If there are any dependent queries to UNION, prepare each of those for
-    // the count query also.
-    foreach ($count->union as &$union) {
-      $union['query'] = $union['query']->prepareCountQuery();
-    }
-
     return $count;
   }
 
