mysql counting occurances in a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sevak316
    New Member
    • Sep 2008
    • 73

    mysql counting occurances in a table

    Hi,

    Here is a table that I am working with:

    PERSON | HAS
    --------------------------------------------------------
    John | Laptop
    John | Cell Phone
    John | PS3
    Sally | Cell Phone
    Sally | Laptop
    Jane | Laptop


    I want my query to return the set of people who own all 3 items (Laptop, Cell Phone, PS3)....

    So in this example above, the only name that should come up is "John"

    One catch, this has to be dynamic, so future items can be added to the list. Basically, it's suppose to count all items in the list and see if a person owns all of them.

    Please help.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Check out the count() function.

    Comment

    • sevak316
      New Member
      • Sep 2008
      • 73

      #3
      Code:
      SELECT person
      FROM my_table
      GROUP BY person
      HAVING Count( person ) = Count( has )
      this is what i have right now and no luck.
      Last edited by Atli; Nov 3 '09, 05:15 AM. Reason: Please use [code] tags when posting code.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        try something like :

        count (has) from my_table where person = john.

        Comment

        • SLauren
          New Member
          • Feb 2009
          • 60

          #5
          You can try something like:

          Code:
          SELECT person FROM my_table GROUP BY person HAVING COUNT(has) = (SELECT COUNT(DISTINCT has) FROM my_table);

          Hope this could help.

          Thanks,
          Lauren

          Comment

          Working...