Reputation: 4462
I have the following query which groups all entries in a MySQL table by the Name column:
"SELECT * FROM visits GROUP by Name"
I use this to generate a row in an html table for each Name returned. I would like to set the rowspan of the first cell in each of these rows (which contains the name) to the count of the number of unique entries for each name in a second column in the same table called "Page", and I presume that I would need to do this using the same query.
So, for example, if there were 50 entries/rows in the MySQL table 'visits' for the Name 'Nick', and five distinct values in the column 'Page' for Nick, the markup generated would be:
<tr><td rowspan="5">Nick<td>....
How would I write the query to achieve both these aims?
Upvotes: 0
Views: 247
Reputation: 52802
You can solve this by using COUNT(DISTINCT column)
.
SELECT Name, COUNT(DISTINCT Page) AS Page_Distinct FROM visits GROUP BY Name
Tested locally, and it gives the correct result.
Upvotes: 2