How to get the distinct number of the nodes? In my task I need to get the distinct number of the nodes of some content type. How can I do it? 

$query = $this-&gt;connection-&gt;select('node', 'node')-&gt;condition('node.type', $type);
$query-&gt;addExpression('COUNT(*)');
$query-&gt;innerJoin('taxonomy_index', $id,  "$id.nid = node.nid  AND $id.status = 1");
$count = intval($query-&gt;execute()-&gt;fetchField());


As the node may have multiple values for the field, in the end it counts all nodes and produces the wrong output. Therefore I need to get distinct number of nodes.

Just adding $query->distinct(); doesn't bring anything. Could you please help me how can I solve the issue?
If you want to retrieve distinct number of the nodes of content type $type.
Try this.  



use Drupal\Core\Database\Database;

//- Retrieves a \Drupal\Core\Database\Connection which is a PDO instance
  $connection = Database::getConnection();
    $query = $connection->select('node', 'node')
        ->fields('node', array('nid'))
        ->condition('node.type', $type, '=')
        //- Force filtering of duplicate records in the result set.
         ->distinct()
        ->countQuery()
        ->execute();

    $count = $query->fetch(\PDO::FETCH_NUM);

 

Ps: you still have to add Your desired innerJoin to the query take a look to Select::innerJoin .