SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    COUNT(*) problem with GROUP BY

    What I'm trying todo is count the total amount of rows that match a series of tags that are related to one or more user_ids.

    I've noticed that the COUNT(*) operation returns the total amount of rows that were calculated for each group by label.

    so:

    lets say that a user_id of 2 belongs to users_tags both in a record where the tag_id is 1 and 2.

    SELECT user_id FROM users_tags WHERE tag_id IN (1,2)

    I'm therefore going to end up getting two results.

    SELECT user_id FROM users_tags WHERE tag_id IN (1,2) GROUP BY user_id.

    Ok now I have 1 record (that's what I want), but when I run the count operation I get:

    SELECT count(*) FROM users_tags WHERE tag_id IN (1,2) LIMIT 1

    which gives me a value of two.

    Now lets say that user_id of 3 also belongs to both tags.

    then I'll get a count(*) of 4, but when I select columns I get two rows (which is the total count that I want).

    How do I count the total amount of rows (using COUNT(*)) with a group by operation for this situation?
    I can't believe I ate the whole thing

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    depends on whether you're counting users or counting tags

    what do you want to count?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm counting users.

    I noticed that when I group by tag_id I get the count value that I want.
    I can't believe I ate the whole thing


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
  •