#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Combining and adding like results


    I have searched all over trying to find away when multiple like records are retreived I need to add together 1 field. My query and results below.

    SELECT m.id as 'id', m.name as 'Merchant Name', c.numStamps as 'Num Stampts
    on Card', COUNT(c.numStamps) as '# Users with this # Stampts'

    FROM Merchants m, Cards c, Stores s

    WHERE c.store_id = s.id

    AND s.merchant_id = m.id

    and c.redeemed = 0

    and c.Numstamps>8

    and c.datelaststamp > '2011-07-16 07:00:00'

    group by m.id, c.numStamps

    order by m.name, c.numStamps

    id Merchant Name Stampts UserStampts

    391 360 Gourmet Burritos 9 3
    391 360 Gourmet Burritos 10 6

    I need to combine id and Merchant Name. Stampts does not really matter but I need to add together user stampts. So I need the return to be

    391 360 Gourmet Burritos 10 9

    Thanks for the help,

    Kane
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,928
    Rep Power
    378
    Code:
    SELECT id, `Merchant Name`, SUM(userstampts) ttl FROM
    (
    my query
    )x
    GROUP BY id;
    ?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    May be you are looking for GROUP_CONCAT.

IMN logo majestic logo threadwatch logo seochat tools logo