1

当检索到多个类似记录时,我已经搜索了所有内容,我需要将 1 个字段加在一起。我的查询和结果如下。

这是我的查询:

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

我需要结合idMerchant NameStampts没关系,但我需要加在一起UserStampts。所以我需要回报是:

391 360 Gourmet Burritos 10 9
4

3 回答 3

1

修改查询的一种简单方法是将其转换为嵌套查询:

SELECT id, Merchant, SUM(Stampts) AS Stampts, SUM(UserStampts) AS UserStampts
FROM ( ... your old query goes here ... ) AS oldquery
GROUP BY id, Merchant;

这将对两列求和(独立)。

于 2012-09-22T21:26:46.287 回答
0

You need to group by the name as well:

group by m.id, m.name, c.numStamps
于 2012-09-23T03:00:17.070 回答
0

尝试这个

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

order by m.name, c.numStamps
于 2012-09-22T21:02:00.667 回答