我在加入 MySQL 时遇到问题。我有 2 张桌子,customerorder 和 customerorderpos。pos 表包含订购的项目 - 因此 customerorder 表中的每个订单始终只有一个条目,但 customerorderpos 中可能有多个具有相同 customerorderid 的条目。
除非我尝试对 customerorder 进行总和计算,否则一切都在查询中有效。例如总和(cart_total_complete)。这将返回重复值(它对具有多个 customerorderpos 记录的订单进行多次计数)。
我确定这是基本的东西,无论是连接类型还是我使用 distinct 的方式,但我已经尝试了几个小时,但无法让它工作......
任何想法我做错了什么?谢谢你的帮助!!
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