Rosen Todorovsky
Rosen Todorovsky

Reputation: 3

Count column values sort by occurence

I have a column in my table users that's called user_model. It contains different reoccurring names as, value1 value2 value3 etc.

I want to count how many times all the different names occur and sort them by largest to smallest value.

How can I do that?

Upvotes: 0

Views: 117

Answers (2)

prasobh
prasobh

Reputation: 95

<?php
$link = mysql_connect('localhost', 'root', '123456');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('testing', $link);

if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
$sql = "select count(a.title),a.title from le7im_menu a, le7im_menu b WHERE a.title = b.title GROUP BY a.title";
$res=mysql_query($sql);
while ($row=mysql_fetch_array($res)){
    $rows[]= $row;
}
print_r($rows);
?>

Upvotes: 0

John Woo
John Woo

Reputation: 263723

SELECT  user_model, COUNT(*) totalCOUNT
FROM    users
GROUP BY user_Model
ORDER BY totalCOunt DESC

Upvotes: 4

Related Questions