我需要疑難問題的幫助。這與Spree Commerce平臺中實現的複雜方式優惠券有關。查詢多個表的總和
我只需要未付優惠券總額。
的表是:
偏好
id | value | key
---+---------------------------------------------
1 | 25 | spree/calculator/flat_rate/amount/5
計算器
id | calculable_id | calculable_type
----+---------------+-----------------
5 | 3 | promotion_action
promotion_actions
id | activator_id
----+-------------
3 | 2
活化劑
個id | expires_at | usage_limit
---+------------+------------
2 | 2013-12-01 | 4
調整
originator_type | originator_id | amount
-----------------+---------------+-------
promotion_action | 3 | -25
在preferences.key最後一個數字對應於計算器的ID。
首先,我需要preferences.amount的總和乘以activators.usage_limit(除非activators.expires_at <今天),其中preferences.key LIKE'%calculator/flat_rate%'。
結果應該是這個數額減去相應adjustments.amount
的總和我得到儘可能
select
(select sum(value) from spree_preferences
where `key` like "%calculator/flat_rate%")
+
(select sum(amount) from spree_adjustments
where originator_type = 'promotion_action') as total;
,但這並不考慮expires_at和usage_limit。
對於更新的大禮包,答案如下:
select sum(subAggregate.outstanding) from ( select (subDetail.value * subDetail.multiplier) + subDetail.adjustmentAmount as outstanding from ( select p.value, case when a.expires_at > curDate() then a.usage_limit else 1 end as multiplier , ifNull(adj.amount,0) as adjustmentAmount from spree_preferences p left outer join spree_calculators c on replace(p.key,'spree/calculator/flat_rate/amount/','') = c.id left outer join spree_promotion_actions pa on c.calculable_id = pa.id and c.calculable_type = 'Spree::PromotionAction' left outer join spree_promotions a on pa.promotion_id = a.id left outer join spree_adjustments adj on pa.id = adj.source_id and pa.type = 'Spree::PromotionAction' ) subDetail ) subAggregate
讓我們看看你已經嘗試什麼 –
這不是一個「做我的工作對我來說」的網站。向我們展示您的代碼無法運行的部分,我們可能會提供幫助。 – Bohemian
我已經展示了我有多遠。如果我對如何解決這個問題有任何線索,我都不會發布這個超長的問題。由於查詢太複雜,我可以完全理解你是否不想提供幫助。 – lafeber