SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    counting how much of somethig there is in a database

    say i have a list of names in my database such as

    id name
    1 john
    2 micheal
    3 micheal
    4 josh
    5 john
    6 john

    and i want count how many johns or micheals there are... displaying something like this

    micheal - 2
    john - 3
    josh - 1


    how would i do that?

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using group by

    select `name`, count(*)
    from ATable
    group by `name`

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but then how would i get that number into a variable and echo it

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query mysql_query("SELECT name FROM table GROUP BY name");

    while (
    $row mysql_fetch_array($query))
    {
      
    $name $row['name'];
      echo 
    $name.'<br />';

    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    use a column alias
    Code:
    select name
         , count(*) as total_rows
      from daTable
    group 
        by name
    then refer to total_rows in your script
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright i got it, thanks

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    619
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a better way

    PHP Code:
    $query mysql_query("SELECT distinct(name) FROM table ");

    while (
    $row mysql_fetch_array($query))
    {
      
    $name $row['name'];
      echo 
    $name.'<br />';


  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    adnasium, where is the count?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    619
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query = mysql_query("SELECT distinct(name) FROM table ");

    while ($row = mysql_fetch_array($query))

    {

    $name = $row['name'];

    # you should be able to use this query and get the total amount of users for that name.
    $query = mysql_query("SELECT count(name) as cnt FROM table where name = '.$row['name'].''");


    echo $name.'<br />';

    }

    Let me know if you still don't get it.

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    adnasium, your solution appears to run a query inside a loop

    would you please try the GROUP BY query in post #5

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    619
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, didn't see that post. That does make more sense then my solution, mine was a really dirty solution. A group by would be a good choice there, i must have thought the data was in another table.

    Here would be the final solution.

    PHP Code:
    $query mysql_query("select name, count(*) as ttl from daTable group by name");
    while (
    $row mysql_fetch_array($query)) echo '<p>'.$row['name'].' ('.$row['ttl'].')</p>'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •