Feb 7, 2013

Posted by in Others | 0 comments

Query to count total number of nodes in a given term in Drupal

Requirement is simple and it complies of 1 query. Note the use of DISTINCT here, as generally we miss it and end-up getting wrong counts, as node-revisions are also taken into account in this case.

WRONG : SELECT COUNT(*) AS cnt, tid FROM term_node GROUP BY tid
CORRECT : SELECT COUNT(DISTINCT(nid)) AS cnt, tid FROM term_node GROUP BY tid

Leave a Reply