sum函数与两个日期范围之间的连接

时间:2015-11-30 20:21:30

标签: mysql date join

朋友们,我有两张包含名称顺序和产品订单的表

CREATE TABLE `order` (
  `order_num` int(11) NOT NULL AUTO_INCREMENT,
  `date` date DEFAULT NULL,
  `time` time DEFAULT NULL,
  `cutomer_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`order_num`)
) ENGINE=InnoDB AUTO_INCREMENT=235 DEFAULT CHARSET=latin1

CREATE TABLE `product_order` (
  `order_num` int(11) NOT NULL,
  `idproduct` int(11) NOT NULL,
  `quantity` tinyint(4) DEFAULT NULL,
  `sub_price` int(11) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `actual_price` int(11) DEFAULT NULL,
  PRIMARY KEY (`order_num`,`idproduct`),
  KEY `fk_product_order_order1_idx` (`order_num`),
  KEY `fk_product_order_product1_idx` (`idproduct`),
  CONSTRAINT `fk_product_order_order1` FOREIGN KEY (`order_num`) REFERENCES `order` (`order_num`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_product_order_product1` FOREIGN KEY (`idproduct`) REFERENCES `product` (`idproduct`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我想加入这两个表并希望检索product_order.actual_price,product_order.sub_price和product_order.quantity的总和 特定日期之间 我的查询返回零行,我不知道如何解决这个问题

这是我的查询

Select `order`.date,
  Sum(product_order.actual_price) as actual_price_sum,
  Sum(product_order.sub_price) as sub_price_sum,
  Sum(product_order.quantity) as sold_quantity,
  (Sum(product_order.sub_price)-Sum(product_order.actual_price)) as profit

From `order`
 Inner Join product_order On `order`.order_num = product_order.order_num

WHERE CAST(`date` AS date) BETWEEN '2014-11-11' and '2015-11-11'

Group By `order`.date          

0 个答案:

没有答案