gpt4 book ai didi

mysql - 合并和添加相似的结果

转载 作者:行者123 更新时间:2023-11-29 00:40:19 25 4
gpt4 key购买 nike

当检索到多个类似记录时,我到处搜索,试图找到我需要加在一起的 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

最佳答案

修改查询的一种简单方法是将其变成嵌套查询:

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

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

关于mysql - 合并和添加相似的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12547628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com