我已经花了很长时间试图为我得到的这个 SUM 查询找到一个解决方案:
SELECT o.id, o.ordernr, o.datetime, o.status, CONVERT(ROUND(o.shipping,2), CHAR(8)) AS shippingcosts,
u.id AS uid, u.name, u.surname, u.address, u.number, u.zipcode, u.country, u.email,
GROUP_CONCAT(CONVERT(op.amount, CHAR(8))) AS amount,
GROUP_CONCAT(CONVERT(ROUND(op.pprice,2), CHAR(8))) AS pprice,
IF(
u.country!= 'NL',
ROUND(SUM(
CASE
WHEN op.discount > 0 THEN ((op.amount * op.pprice) - ((op.amount * op.pprice) * (op.discount/100)))
WHEN op.discountf > 0 THEN ((op.amount * op.pprice) - ((op.amount * op.pprice) * (op.discountf/100)))
WHEN op.discountf = 0 && op.discount = 0 THEN (op.amount * op.pprice)
END
) + o.shipping,2),
ROUND((SUM(
CASE
WHEN op.discount > 0 THEN ((op.amount * op.pprice) - ((op.amount * op.pprice) * (op.discount/100)))
WHEN op.discountf > 0 THEN ((op.amount * op.pprice) - ((op.amount * op.pprice) * (op.discountf/100)))
WHEN op.discountf = 0 && op.discount = 0 THEN ((op.amount * op.pprice)*1.19)
END
)*1.19) + o.shipping,2)
) AS total,
GROUP_CONCAT(CONVERT(p.id, CHAR(8))) AS pid,
SUM(p.weight) AS weight,
FROM orders AS o
INNER JOIN users AS u ON o.uid = u.id
INNER JOIN order_products AS op ON op.oid = o.id
INNER JOIN products AS p ON op.pid = p.id
GROUP BY o.id
我想要实现的是,如果 op.discount 不为空或 op.discountf 不为空,并且对于 op.discount 和 op.discountf 都为空的每一行,则获取每一行的总和......
在这一点上,mysql似乎正在对所有总数求和..??
有人可以帮我一把吗??
Thnx提前(很多)