MySQL显示总和0

时间:2013-03-28 01:10:00

标签: mysql

所以我有一个包含一组订单商品的表,订单商品表会跟踪特定订单中包含的特定商品的数量。下面是该示例中使用的表的简化版本。

订单商品表

order_item_id : int
order_item_item_id : int
order_item_quantity : int
order_item_order_id : int

订购表

order_id : int
order_date : date

我正在尝试执行单个查询,该查询将平均每周,每月和所有时间的特定order_item_item_id的销售额。

目前我的查询如下

SELECT totalAvg.item 

ROUND(AVG(totalAvg.total)+0.4999999, 0) AS totalAverage,  
ROUND(AVG(monthAvg.total)+0.4999999, 0) AS monthAverage, 
ROUND(AVG(weekAvg.total)+0.4999999, 0) AS weekAverage from

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total,
 DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id = 
`order`.order_id GROUP BY DATE(`order`.order_date), order_item_item_id) AS totalAvg,

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total, 
DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id =
`order`.order_id AND `order`.order_date > date_sub(now(), interval 1 week) GROUP BY 
DATE(`order`.order_date), order_item_item_id) AS weekAvg,

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total,
DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id = 
`order`.order_id AND `order`.order_date > date_sub(now(), interval 1 month) GROUP BY 
DATE(`order`.order_date), order_item_item_id) AS monthAvg,

WHERE totalAvg.item = monthAvg.item AND
monthAvg.item = weekAvg.item 
GROUP BY item

问题在于,如果weekAvg表中不存在某个项,则不会为totalAvg或monthAvg打印任何结果。我怎样才能在一个声明中做到这一点?

示例数据

{order_id : 5, order_date : 02/02/2013}
{order_id : 6, order_date : 13/03/2013}

{order_item_id : 1, order_item_order_id : 5, order_item_item_id : 1, order_item_quantity : 3}
{order_item_id : 2, order_item_order_id : 6, order_item_item_id : 1, order_item_quantity : 4}

当前输出没有返回任何内容,因为每周报告没有ID为1的订单商品的条目。我试图解决这个问题,以便它输出以下内容

totalAverage = 3.5
monthAverage = 4
weekAverage = 0

值的计算方法如下:

总平均值=特定商品的总数除以商品的销售天数。

月份和周值与之前的计算相同,但有时间限制,因此订单必须放在上周/月内。

1 个答案:

答案 0 :(得分:0)

您需要使用outer joins将totalAvg连接到monthAvg和weekAvg。您可以使用COALESCE()确保为空周或月值获得0而不是NULL。

您还应该使用ANSI连接语法。它不易出错,更易读。

尝试这样的事情:

SELECT totalAvg.item,
ROUND(AVG(totalAvg.total)+0.4999999, 0) AS totalAverage,  
COALESCE(ROUND(AVG(monthAvg.total)+0.4999999, 0),0) AS monthAverage, 
COALESCE(ROUND(AVG(weekAvg.total)+0.4999999, 0),) AS weekAverage 
from
(
  SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total,
    DATE(`order`.order_date) AS date 
  FROM orderitem
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
  GROUP BY DATE(`order`.order_date), order_item_item_id
) AS totalAvg
left outer join
(
  SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total, 
    DATE(`order`.order_date) AS date 
  FROM orderitem
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
  WHERE `order`.order_date > date_sub(now(), interval 1 week) 
  GROUP BY 
  DATE(`order`.order_date), order_item_item_id
) AS weekAvg on weekAvg.item = totalAvg.item
left outer join
(
  SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total,
    DATE(`order`.order_date) AS date 
  FROM orderitem
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
  WHERE `order`.order_date > date_sub(now(), interval 1 month) 
  GROUP BY DATE(`order`.order_date), order_item_item_id
) AS monthAvg on monthAvg.item = totalAvg.item
GROUP BY item