0

我正在尝试创建一个查询,该查询将在从初始值中扣除一些折扣后给我一个产品的值。

这是我到目前为止的简化版本:

SELECT
p.price
GROUP_CONCAT(d.amount) AS discounts
FROM products p
LEFT OUTER JOIN discounts d
ON d.product_id = p.id
GROUP BY p.id

其中 discounts.amount 是十进制值。

编辑:例如,如果有两个对应的折扣分别为 10 和 15,并且初始产品价格为 50,则计算将返回 25

无论如何,在查询中我可以执行我想要的计算吗?

任何建议将不胜感激。

谢谢。

4

1 回答 1

1

有一个 SUM() 方法:

SELECT p.price,
       p.price - COALESCE(SUM(d.amount), 0) as discounted_price,
       GROUP_CONCAT(d.amount) AS discounts
FROM products p
LEFT OUTER JOIN discounts d
ON d.product_id = p.id
GROUP BY p.id
于 2011-05-27T15:36:03.383 回答