在一个查询中,我需要分别按按日期过滤的产品分别对“购买和订购”组中每种产品的体积求和。
例如,我需要知道到2017年和第12个月我购买了多少商品以及订购了多少商品。
赞:
这是产品表,用于存储产品的详细信息。
CREATE TABLE t_product
(
pkey INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
cname VARCHAR(99) NOT NULL,
);
这是购买表,用于存储购买的基本详细信息,例如账单ID和日期。
CREATE TABLE t_purchase
(
pkey INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
cdated DATETIME NOT NULL,
);
这是“购买详细信息”,其中存储了所购买的每个产品的详细信息
CREATE TABLE t_purchase_detail
(
pkey INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
rpurchase INT(10) unsigned NOT NULL,
rproduct INT(10) unsigned NOT NULL,
crate DECIMAL(10,2) unsigned NOT NULL,
cdiscount DECIMAL(5,2) unsigned NOT NULL,
ctax DECIMAL(5,2) unsigned NOT NULL,
cvolume INT(10) unsigned NOT NULL,
CONSTRAINT t_purchase_detail_ibfk_2 FOREIGN KEY (rpurchase) REFERENCES t_purchase (pkey),
CONSTRAINT t_purchase_detail_ibfk_3 FOREIGN KEY (rproduct) REFERENCES t_product (pkey)
);
CREATE INDEX rproduct ON t_purchase_detail (rproduct);
CREATE UNIQUE INDEX rpurchase ON t_purchase_detail (rpurchase, rproduct);
这是订单表,其中存储了已接收订单的基本详细信息,例如订单ID和日期
CREATE TABLE t_order
(
pkey INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
cdated DATETIME NOT NULL,
);
这是订单明细,其中存储了所订购的每个产品的详细信息
CREATE TABLE t_order_detail
(
pkey INT(10) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
rproduct INT(10) unsigned NOT NULL,
rorder INT(10) unsigned NOT NULL,
csale DECIMAL(8,2) unsigned NOT NULL,
cdiscount DECIMAL(5,2) unsigned DEFAULT '0.00' NOT NULL,
ctax DECIMAL(5,2) unsigned DEFAULT '0.00' NOT NULL,
cvolume SMALLINT(5) unsigned NOT NULL,
CONSTRAINT t_order_detail_ibfk_1 FOREIGN KEY (rorder) REFERENCES t_order (pkey),
CONSTRAINT t_order_detail_ibfk_2 FOREIGN KEY (rproduct) REFERENCES t_product (pkey)
);
CREATE UNIQUE INDEX roder ON t_order_detail (rorder, rproduct);
CREATE INDEX rproduct ON t_order_detail (rproduct);
编辑
这是我正在运行的查询
SELECT
t_product.cname AS name,
sum(t_purchase_detail.cvolume) AS volume,
sum(t_order_detail.cvolume) AS sold
FROM t_product, t_purchase, t_purchase_detail, t_order, t_order_detail
WHERE t_purchase.cgst = 1
AND (t_product.pkey = t_purchase_detail.rproduct AND t_product.pkey = t_order_detail.rproduct)
AND (t_purchase.pkey = t_purchase_detail.rpurchase AND t_order.pkey = t_order_detail.rorder)
AND (
(YEAR(t_purchase.cdated) <= 2017 AND MONTH(t_purchase.cdated) <= 12)
AND
(YEAR(t_order.cdated) <= 2017 AND MONTH(t_order.cdated) <= 12)
)
GROUP BY t_product.pkey
ORDER BY name;
这是从“采购详细信息”和“订单详细信息”表中选择常用产品。但是在某些情况下,某些产品仅在“购买明细”表中可用,而某些产品仅在“订单明细”表中可用。如果某产品在某个表中缺失但在另一表中缺失,则该表中的体积应计算为0。例如,如果购买了总共30单位的Orange Facewash,但没有订购,则输出应为>
反之亦然。
编辑2
此查询返回所有产品,包括两个列表中都不存在的产品。
SELECT
t_product.pkey AS key_s,
t_product.cname AS NAME,
(SELECT sum(t_purchase_detail.cvolume)
FROM t_product, t_purchase, t_purchase_detail
WHERE t_product.pkey = key_s
AND t_product.pkey = t_purchase_detail.rproduct
AND t_purchase.pkey = t_purchase_detail.rpurchase
AND YEAR(t_purchase.cdated) <= 2017 AND MONTH(t_purchase.cdated) <= 12
GROUP BY t_product.pkey) AS volume,
(SELECT sum(t_order_detail.cvolume) AS sold
FROM t_product, t_order, t_order_detail
WHERE t_product.pkey = key_s
AND t_product.pkey = t_order_detail.rproduct
AND t_order.pkey = t_order_detail.rorder
AND YEAR(t_order.cdated) <= 2017 AND MONTH(t_order.cdated) <= 12
GROUP BY t_product.pkey) AS sold
FROM t_product
GROUP BY t_product.pkey
ORDER BY NAME;
我需要一种在干净的单个查询中更有效地编写此代码的方法,该查询可以删除“详细信息”表中没有的产品。
谢谢