I have a table with a
rank column which can be either
3. I need to count the number of records for each rank, but I also need to display all the data ordered by rank.
From my perspective, the two ways to accomplish this would be:
Pull out the data with a normal
SELECTand then run a second query to obtain counts:
SELECT rank, COUNT(id) FROM tablename GROUP BY rank
- Pull out the data with a normal SELECT and then iterate twice over the result set I get: once to count the occurrences of each rank, and the second time to actually display the data.
Which way would be more performant? Does it depend on table size? I imagine for large tables the
SELECT COUNT would be a tad slower than counting in (PHP, ASP.NET, Java), in particular if there's more than 3 discrete values I want to count.