我不是SQL专业人员,所以也许我正在使事情复杂化,但这就是问题所在。我的商店系统有5张桌子。
商品表保存有关店内所有物品的信息。
create table goods(
id int unsigned not null auto_increment,
name varchar(255) not null,
code varchar(20) not null unique,
sellingPrice float,
primary key(id)
);
收据表保存有关购买的信息。
create table receipt(
id int unsigned not null auto_increment,
company varchar(255) not null,
identifier varchar(50) unique,
date date not null,
price float not null,
primary key(id)
);
issue_card 表保存有关购买的信息。
create table issue_card(
id int unsigned not null auto_increment,
identifier varchar(50) unique,
receiver varchar(255),
date date not null,
primary key(id)
);
商品和收据表已连接(一次购买了多个商品)
create table goods_receipt (
goods_id int unsigned not null,
receipt_id int unsigned not null,
amount int unsigned not null,
inp_price float not null,
foreign key (goods_id) references goods(id),
foreign key (receipt_id) references receipt(id),
CONSTRAINT goods_receipt_unique UNIQUE (goods_id, receipt_id)
);
商品和 issue_card 已连接(一次售出多种商品)
create table issue_card_goods (
goods_id int unsigned not null,
issue_card_id int unsigned not null,
amount int unsigned not null,
foreign key (goods_id) references goods(id),
foreign key (issue_card_id) references issue_card(id),
CONSTRAINT goods_receipt_unique UNIQUE (goods_id, issue_card_id)
);
我需要找出的是在给定时间分别存储的每个项目的总金额。
示例: 2015年1月4日-商店里有100个橘子(到那天可能买了100,000个,卖了99,900个),还有1000个香蕉,...
到目前为止,我设法做到的是:
通过创建此视图来获得以上结果(计数所有项目,但没有时间限制)
CREATE VIEW store_items
AS
SELECT main.id,
main.NAME,
main.code,
Ifnull(main.bought, 0) - Ifnull(sec.sold, 0) AS available,
main.sellingprice
FROM (SELECT goods.id,
goods.NAME,
goods.code,
Sum(goods_receipt.amount) AS bought,
goods.sellingprice
FROM goods
LEFT JOIN goods_receipt
ON goods.id = goods_receipt.goods_id
GROUP BY code) AS main
JOIN (SELECT goods.code,
Sum(issue_card_goods.amount) AS sold
FROM goods
LEFT JOIN issue_card_goods
ON goods.id = issue_card_goods.goods_id
GROUP BY code) AS sec
ON main.code = sec.code;
考虑到时间限制,我创建了2个视图。在第一个中,我将收据与货物合并在一起。
CREATE VIEW store_items_sold_date
AS
SELECT goods.id AS id,
goods.code AS code,
goods.NAME AS NAME,
giss.amount AS amount,
giss.date AS date_sold
FROM (SELECT *
FROM issue_card
LEFT JOIN issue_card_goods
ON issue_card.id = issue_card_goods.issue_card_id) AS
giss
LEFT JOIN goods
ON giss.goods_id = goods.id;
哪个给我日期和售出的每个物品的数量。
第二个视图类似,仅用于购买(将issue_card与商品一起使用)
CREATE VIEW store_items_bought_date
AS
SELECT goods.id AS id,
goods.code AS code,
goods.NAME AS NAME,
gr.amount AS amount,
gr.date AS date_bought
FROM (SELECT *
FROM receipt
LEFT JOIN goods_receipt
ON receipt.id = goods_receipt.receipt_id) AS gr
LEFT JOIN goods
ON gr.goods_id = goods.id;
现在,我可以获得每个商品的总销售量和购买量。
SELECT id,
code,
NAME,
Sum(amount) AS bought
FROM store_items_bought_date
WHERE date_bought < "2015-04-05"
GROUP BY id;
和
SELECT id,
code,
NAME,
Sum(amount) AS sold
FROM store_items_sold_date
WHERE date_sold < "2015-04-05"
GROUP BY id;
我所缺少的是如何将这些结果结合在一起。我在这里找到了这个查询
(SELECT ... FROM tbl1 LEFT JOIN tbl2 ...) UNION ALL (SELECT ... FROM tbl1 RIGHT JOIN tbl2 ... WHERE tbl1.col IS NULL)
但是我未能将其映射到上述结果,问题是在tbl2
处使用了SELECT语句而不是表。任何帮助,将不胜感激。