Count and group same rows

Hello!

I’m trying to group same rows and count them. So far I managed to group them but I want to count them too. I mean output the number as well.

Now the output look like this: Jane

but I want something like this: Jane (14)

<?php
$sql = ' SELECT firs_name, GROUP_CONCAT(first_name) as id FROM people GROUP BY first_name ORDER BY count(id) DESC';
 foreach ($db->query($sql) as $row) {
     echo '<tr><td> '.$row['first_name'].' </td>  </tr>';
 }
 ?>

SELECT CONCAT(first_name, ' (', COUNT(*), ') ') FROM people GROUP BY first_name ORDER BY COUNT(*) DESC

Unfortunately this does not work for me :confused:

i am unfamiliar with the “does not work for me” error message

please, do describe exactly what happened

I copy pasted your solution and this is what I get.

Parse error: syntax error, unexpected ‘(’ in C:\xampp\htdocs\tartalomkezelo\admin\index.php on line 137

<?php
$sql = ' SELECT CONCAT(first_name, ' (', COUNT(*), ') ')  FROM people  GROUP BY first_name ORDER  BY COUNT(*) DESC';
 foreach ($db->query($sql) as $row) {
     echo '<tr><td> '.$row['first_name'].' </td>  </tr>';
 }
 ?>

that’s not an sql error – that’s a php error

run the query directly in mysql, not from php, to confirm that it works

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.