我在MySQL中遇到问题。我有2个表,customerorder和customerorderpos。 pos表包含已订购的项目 - 因此在customerorder表中每个订单总是只有一个条目,但是可能有几个在customerorderpos中具有相同的customerorderid。
除了我尝试对客户端进行求和计算外,查询中的所有内容都有效。例如sum(cart_total_complete)。这将返回重复值(对于具有多个customerorderpos记录的订单,它会多次计数)。
我确定它是基本的东西,无论是使用连接类型还是我如何使用不同的东西,但我已经尝试了几个小时而且无法让它发挥作用。 ..
任何想法我做错了什么? 谢谢你的帮助!!
select concat(left(dayname(from_unixtime(customerorder.datetime)),3), ' ',
day(from_unixtime(customerorder.datetime))) as day,
count(distinct customerorder.customerorderid) as count_totalorders,
count(distinct customerorderpos.itemid) as count_differentitems,
sum(customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled) as quantity_ordered,
sum(customerorderpos.itemsubtotal) as item_subtotal,
sum(customerorderpos.pricechangetotal) as item_pricechangetotal,
sum(customerorderpos.itemtotal) as item_total,
sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) as item_purchasepricetotal,
sum(cart_discounttotal) as total_discount,
sum(customerorderpos.itemtotal) - sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) - sum(cart_discounttotal) as item_earningstotal,
sum(cart_total_shipping) as total_shipping,
sum(cart_total_tax) as total_tax,
sum(cart_total_complete) as total_complete
from customerorder inner join customerorderpos on customerorderpos.customerorderid = customerorder.customerorderid
where customerorder.status_cancelled = 0
group by day(from_unixtime(customerorder.datetime)) order by customerorder.datetime
答案 0 :(得分:0)
如果您首先按customerorderpos
汇总customerorderid
表:
SELECT customerorderid,
SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
SUM(itemsubtotal) AS item_subtotal,
SUM(pricechangetotal) AS item_pricechangetotal,
SUM(itemtotal) AS item_total,
SUM(purchase_price * (
quantity_ordered - quantity_cancelled
)) AS item_purchasepricetotal,
FROM customerorderpos
GROUP BY customerorderid
然后,您可以将结果加入customerorder
表:
SELECT FROM_UNIXTIME(customerorder.datetime, '%a %e') AS day,
COUNT(*) AS count_totalorders,
SUM(i.quantity_ordered) AS quantity_ordered,
SUM(i.item_subtotal) AS item_subtotal,
SUM(i.item_pricechangetotal) AS item_pricechangetotal,
SUM(i.item_total) AS item_total,
SUM(i.item_purchasepricetotal) AS item_purchasepricetotal,
SUM(o.cart_discounttotal) AS total_discount,
SUM(
i.item_total
- i.purchasepricetotal
- o.cart_discounttotal
) AS item_earningstotal,
SUM(o.cart_total_shipping) AS total_shipping,
SUM(o.cart_total_tax) AS total_tax,
SUM(o.cart_total_complete) AS total_complete
FROM customerorder o JOIN (
SELECT customerorderid,
COUNT(DISTINCT itemid) AS count_differentitems,
SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
SUM(itemsubtotal) AS item_subtotal,
SUM(pricechangetotal) AS item_pricechangetotal,
SUM(itemtotal) AS item_total,
SUM(purchase_price * (
quantity_ordered - quantity_cancelled
)) AS item_purchasepricetotal,
FROM customerorderpos
GROUP BY customerorderid
) i USING (customerorderid)
WHERE o.status_cancelled = 0
GROUP BY FROM_UNIXTIME(o.datetime, '%e')
ORDER BY o.datetime
请注意,我已删除count_differentitems
,因为不清楚是否要汇总每个订单中不同商品的数量(这将是扩展上述内容的简单案例)或者如果您希望所有聚合订单中的不同项目数(这需要额外的表连接)。
另请注意,我已使用FROM_UNIXTIME()
的日期格式参数代替您手动尝试创建格式化日期字符串。