I have a table and I want to sort by how many duplicates are in the table.
For example,
Table like:
Code:
## id ## name ##
#################
## 1 ## Bob ##
## 2 ## Bob ##
## 3 ## Bob ##
## 4 ## Tom ##
## 5 ## Mike ##
## 6 ## Mike ##
## 7 ## Joe ##
## 8 ## Joe ##
I need that to group by the name and sort it by how many duplicats there are. Any help?
Last edited by NoEffinWay; 05-31-2012 at 08:07 AM.
SELECT name, COUNT(*) AS name_count
FROM the_table
GROUP BY name
ORDER BY name_count DESC
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Yeah, that would sort the way I wanted. However, i forgot to mention I also needed the count of how many each one repeated. I ended up doing something like:
PHP Code:
<?php $query = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($query)){ $allnames[$row['name']][] = $row['name']; } arsort($allnames); foreach($allnames as $an){ $num = count($an); echo $an[0]." shows up ".$num." times in the table."; }
Bookmarks