|id |trans_id| entry_id |type | Price
------------------------------------------------------
|43575855 |24419612| 26898343 |regOption| 139
|43575856 |24419612| 26898343 |xnCoupon | 50
|43575857 |24419612| 26898346 |regOption| 139
|43575858 |24419612| 26898346 |Tshirt | 10
在第二个例子中,想要的数量将是278
|id |trans_id| entry_id |type | Price
------------------------------------------------------
|43575855 |24419612| 26898343 |regOption| 139
|43575856 |24419612| 26898343 |xnCoupon | 50
|43575857 |24419612| 26898346 |regOption| 139
|43575858 |24419612| 26898346 |xnCoupon | 50
我的尝试就像这样
Select sum(price) FROM table where type ='regOption' AND (something)
检查是否与相同的entry_id与xnCoupon相关的regOption?
有人可以帮我逻辑,欢迎任何帮助
答案 0 :(得分:1)
SELECT SUM(price)
FROM table
WHERE type = 'regOption'
AND entry_id IN
(
SELECT entry_id
FROM table
WHERE type = 'xnCoupon'
)
答案 1 :(得分:1)
这可能不是您所期待的 - 此声明将regOption类型的价格与每笔交易的相应xnCoupon相加并给出总计。
select ifnull(reg.trans_id,'<<TOTAL>>') as trans_id,
sum(reg.price)
from `table` reg
join `table` xnc
on ( xnc.entry_id = reg.entry_id
and reg.type = 'regOption'
and xnc.type = 'xnCoupon')
group by reg.trans_id with rollup;