September 21st, 2012, 02:29 AM
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.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,
September 21st, 2012, 04:03 AM
SELECT id, `Merchant Name`, SUM(userstampts) ttl FROM
GROUP BY id;
September 22nd, 2012, 02:27 PM
May be you are looking for GROUP_CONCAT.