SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find, Count and Print?!?

    I'm trying to figure out how to only print the record id with most counts depending on a specific variable.

    I have an db table with users which has a country id and state id attached to them like this:

    id, name, country, state

    Now I want to loop through this table and find out which state has the most users depending on the country.

    Lets say I specify the country like this:
    PHP Code:
    $country 13
    How do I Print the state with the most records?!?

    Hope this is to figure out... Thanks in advance!

  2. #2
    SitePoint Zealot
    Join Date
    May 2002
    Location
    RI-USA
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This might be more of a SQL question, and hopefully you're using mysql (as that's what I used to test this query). But this should do what you want:

    Code MySQL:
    SELECT country, state, count(state) as cnum FROM `table` WHERE `country` = '$country' GROUP BY `state` ORDER BY `cnum` DESC
    Jim

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect man... Works like a charm

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    note that if you have a WHERE condition that restricts the results to only one country, you should remove country from the SELECT clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •