How to count unique values?

Discussion in 'Databases' started by Grumps, Oct 21, 2007.

  1. #1
    Is there a way to do this without loops? I remember doing it before but cant remember the exact command line.

    Lets say i have column called 'cat'

    Values
    Cat1
    Cat2
    Cat1
    Cat3
    Cat1
    Cat3

    how do i list all of the above with distinct and also to count them.

    eg:
    Cat1 = 3
    Cat2 = 1
    Cat3 = 2
     
    Grumps, Oct 21, 2007 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    select distinct cat from ...
    and then
    
    $numrows=mysql_num_rows($result)
    
    Code (markup):
     
    maiahost, Oct 21, 2007 IP
  3. Grumps

    Grumps Peon

    Messages:
    592
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dont think it will work because distinct will list the item in like

    Cat1
    Cat2
    Cat3

    and if we use mysql_num_rows, it will return 3 rows. Isnt it?
     
    Grumps, Oct 21, 2007 IP
  4. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it will return (count) what you select and if you don't need the cat name but value just replace cat with the field you're interested in.

    Sorry I see what you mean ... then it's a nested query I am afraid
    Select the distinct values and :
    
    while $row=mysql_fetch_assoc($result)
    {
    $cat=$row["cat"];
    $result2=mysql_query("select cat from ... where cat='$cat'");
    $numrows=mysql_num_rows($result2);
    }
    
    Code (markup):
    The above code is quick and dirty so please re-write it :)
     
    maiahost, Oct 21, 2007 IP
  5. Grumps

    Grumps Peon

    Messages:
    592
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah i know the loop method but i thought there is another quicker way. Are there any?

    Hmmp thanks anyway!
     
    Grumps, Oct 21, 2007 IP
  6. upl8t

    upl8t Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    SELECT Cat, COUNT(*) AS CatCount FROM TABLE GroupBy Cat
     
    upl8t, Oct 22, 2007 IP
    tandac likes this.
  7. mbnaragund

    mbnaragund Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    SELECT cat||' = '||count(cat) FROM TABLE GROUP BY cat
     
    mbnaragund, Oct 23, 2007 IP
  8. mhaye_01

    mhaye_01 Banned

    Messages:
    395
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    distinct just eliminate the duplicate data..

    you will retrieve cat's rows and count all data of it and display the column as CatCount
     
    mhaye_01, Oct 25, 2007 IP
  9. N_F_S

    N_F_S Active Member

    Messages:
    2,475
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    90
    #9
    maybe:

    SELECT COUNT(DISTINCT cat) FROM TABLE;

    or:

    SELECT COUNT(DISTINCT cat) AS CatCount FROM TABLE;
     
    N_F_S, Oct 25, 2007 IP