0

我在加入 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
4

1 回答 1

0

如果您首先按以下方式汇总customerorderposcustomerorderid

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()代替您手动尝试创建格式化的日期字符串。

于 2012-11-06T18:21:35.013 回答