SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Deadwood, SD USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count items in two fields

    Hi,

    I have 2 fields to count items in. Field-1 has item-a, item-b, etc. and field-2 has items (item-1, item-2, etc) that group with each item in field-1.
    How do I quary and echo to count the number of each item (item-1, item-2, etc) in field-2 that are grouped with the items in field-1.
    I know about the count function but not sure how to use it for this.
    The table should look like this

    item-a item-1 count of item-1
    item-a item-2 count of item-2
    item-b item-1 count of item-1
    etc. etc etc

    Thanks for the help

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code MySQL:
    SELECT
        field-1
      , field-2
      , COUNT(*) AS total
    FROM tablename
    GROUP BY 
        field-1
      , field-2
    ORDER BY
        field-1
      , field-2

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Deadwood, SD USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido,

    I want to list all items once in field-1 and list and count all items in field-2.
    so should I write it COUNT(field-2)?
    When I set it up the way you showed all items in field-2 count showed 1 even though some items were more than one.
    I am writing my table echo like this;

    echo "<tr><td>";
    echo $row['field-1'];
    echo "</td><td>";
    echo $row['field-2'];
    echo "</td><td>";
    echo COUNT(field-2);
    echo "</td></tr>";

    it shows this;

    field-1 field-2 1

    for all items

    What am I doing wrong?


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
  •