Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2004
    Posts
    11

    Exclamation Unanswered: count 2 value in a table

    hi all pls kindly help
    need to know how to do a count for 2 value in a column

    example :
    my table(entries)
    has the field name 'selection'
    under this selection, data inside have is e.g(apple,apple,pear,pear,pear,orange)
    i need to do a COUNT on how many apple and pear there is inside this table

    but i seriously have no idea, pls kindly help thanks

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    This sounds like homework from an "Introduction to databases" course, but I'm willing to give you the benefit of the doubt...
    Code:
    SELECT Count(*), selection
       FROM entries
       GROUP BY selection
       ORDER BY Count(*) DESC -- gratis
    -PatP

  3. #3
    Join Date
    Dec 2004
    Posts
    11

    thanks for replying

    SELECT Count(*), selection
    FROM entries
    GROUP BY selection
    ORDER BY Count(*) DESC -- gratis


    sorry for my ignorance..
    wat u meant by DESC over here??

    wat i want is to count how many pears and apple there is in that table but not orange

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    DESC means descending, and you don't have to use it if you don't want to

    did you want the total of apples and pears? that's different --

    SELECT count(*) as applesandpears
    FROM entries
    WHERE selection in ('apple','pear')
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Dec 2004
    Posts
    11
    thanks so much....
    really so basic

    need to read up my books again

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Sorry, my background is showing

    Gratis is a Latin vulgate term that means "free of charge" or "complimentary". When I used it in a comment, I meant that I expected it to make things easier to use, without actually impacting the solution at all.

    As Rudy pointed out, in this case it simply orders the result set so that the most frequently occuring items appear at the top of the list. You can safely ignore it if it doesn't help you.

    -PatP

  7. #7
    Join Date
    Dec 2004
    Posts
    11
    ok got it ..
    but still appreciated

Posting Permissions

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