0

i have a table in my mysql database named (names) now everyone can save their real names

now i want to query this table and find out how many times these names used

forexample the output should be :

Jakob (20) Jenny (17)

now this is my own code :

    list($usernames) =mysql_fetch_row(mysql_query('SELECT name FROM table_user GROUP BY name ORDER BY COUNT(name) DESC LIMIT 50 '));
    list($c) =mysql_num_rows(mysql_query('SELECT COUNT(name) FROM table_user GROUP BY name '));

    print $usernames.'('.$c.')'

is this a correct approach ?!

1
  • Why are u using DESC LIMIT 50? Commented Apr 27, 2010 at 12:21

1 Answer 1

5

You can use one select query for this:

$sel_query=mysql_query("SELECT COUNT(name) AS nums, name FROM table_user GROUP BY name");

For outputting, you can use something like this:

while($fetch=mysql_fetch_array($sel_query))
{
   echo $fetch['name']."( ".$fetch['nums']." ) ";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.