Knowledgebase

Counting, Summing and Grouping Print

  • databasesphpmyadmin, databases, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Aggregate queries.

COUNTING

SELECT COUNT(*) FROM table;

With a condition:

SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish';

That is how you audit content: how many published posts, how many products, how many orders.

SUMMING

SELECT SUM(column) FROM table WHERE condition;

Useful for order totals over a period.

GROUPING

SELECT post_type, COUNT(*) FROM wp_posts GROUP BY post_type;

That returns a count per type, which immediately shows how much of the table is revisions.

It is one of the most useful diagnostic queries available.

ORDERING THE RESULT

Add ORDER BY the count descending to see the largest groups first.

WHY THIS MATTERS

Before cleaning up, count what you are about to remove. A count is safe and tells you the scale.

Running COUNT with the same WHERE clause as a planned DELETE is the discipline that prevents accidents.

OTHER AGGREGATES

AVG, MIN and MAX, all following the same pattern.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot