Question

I'm partially familiar with the COUNT function, but I'm a little stumped as to how I can apply it to this specific situation. Suppose I have a table with one of the columns containing the following values:

apples
apples
apples
oranges
oranges
bananas

These values can be added by users, so I never know what values may end up in this column, but there will always be duplicates. What I first want to do is to display them in a HTML table, so that each row will represent one particular value, and each adjacent cell shows the number of duplicate values. Using the example data above, the expected output in a HTML table generated by PHP would be:

|Fruits     | Amount   |
|-----------|----------|
|apples     | 3        |
|oranges    | 2        |
|bananas    | 1        |

At this stage, I know that I have to use the COUNT function in SQL, and through PHP, I need to run some sort of foreach loop, but beyond that, I'm not sure how to proceed.

EDIT: Changed the data. Hopefully it makes a little more sense

Was it helpful?

Solution

SELECT [column_name], COUNT([column_name])
FROM [table_name]
GROUP BY [column_name];

OTHER TIPS

Add GROUP BY valueName; to the end of your query.

Select `column name`, count(*) from `table1`
group by `column name`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top