How to get total number of Occurrences in mysql

Discussion in 'MySQL' started by Amilo, Jan 30, 2007.

  1. #1
    I would like to get a result that is the total number of Occurrences in a column.

    SELECT * FROM mydata WHERE pet=monkey

    Will show me all the monkey results in the pet column,but how do I get a result like 6,if there are 6 results for monkey ?
     
    Amilo, Jan 30, 2007 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    SELECT count(*) FROM mydata WHERE pet=monkey
    PHP:
     
    dct, Jan 30, 2007 IP
  3. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks DCT.

    Do you have an idea how to do this in php:eek:
     
    Amilo, Jan 30, 2007 IP
  4. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #4
    <?php
    $q="select * from table where pet='monkey'";
    $res=mysql_query($q);
    $count = mysql_num_rows($res);
    echo $count;
    ?>
    PHP:
     
    technoguy, Jan 30, 2007 IP
  5. jpcesar

    jpcesar Peon

    Messages:
    243
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you are using .net and bind the result to a datalist, you can use datalistvar.Items.Count

    This way you don't need to make 2 separate queries.

    I can get the results count in PHP using the returned array length property I think, without having to make 2 queries.
     
    jpcesar, Jan 30, 2007 IP
  6. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks technoguy worked great,I have something to build on now to learn how to do more math on the database.
     
    Amilo, Jan 30, 2007 IP
  7. Perrow

    Perrow Well-Known Member

    Messages:
    1,306
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Using the "AS" keyword you can name the value returned by count.
    select count(*) as numPets from table where pet=monkey
    Code (markup):
    If you want to know the number of pets for more than one pet you write
    select count(petID) as numPets, pet from table group by pet
    Code (markup):
    Each row in the result will have the name and count for the pet.
     
    Perrow, Jan 30, 2007 IP
  8. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How would i do this if I had 3 columns: (pet,animal,zoo) and I wanted a total for monkey from all 3 columns ?
     
    Amilo, Jan 30, 2007 IP
  9. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #9
    <?php
    $q="select * from table where pet='monkey' || animal='monkey' || zoo='monkey'";
    $res=mysql_query($q);
    $count = mysql_num_rows($res);
    echo $count;
    ?>
    PHP:
    you are requested to add rep.

    thanks
     
    technoguy, Jan 30, 2007 IP
    Amilo likes this.
  10. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks technoguy ,rep added ;)
     
    Amilo, Jan 31, 2007 IP
  11. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #11
    thank you and for any problem in php you can contact me. Make thread here or just pm me.
     
    technoguy, Jan 31, 2007 IP