Sai*_*han 13 database structure
我想为我的库存创建一个小型数据库,但我在挑选结构时遇到了一些问题.库存将在每天结束时每天更新.
我面临的问题如下.
我有一张桌子供我的产品使用
id, name, price, quantity.
Run Code Online (Sandbox Code Playgroud)
现在我有另一张桌子供我销售,但有我的问题.我需要什么样的领域.在一天结束时,我想存储这样的记录:
20 product_x $ 5,00 $ 100,-
20 product_y $ 5,00 $ 100,-
20 product_z $ 5,00 $ 100,-
20 product_a $ 5,00 $ 100,-
-------------------------------------------------
$ 400,-
Run Code Online (Sandbox Code Playgroud)
那么我该如何在销售记录中对此进行建模.我是否只使用产品ID的逗号分隔创建连接记录.
或者是否有另一种方式以正确的方式对此进行建模.
Waj*_*ghe 42
这是一个支持许多方面的模型,
希望这会有所帮助.如果您需要有关每张桌子的更多信息,请与我们联系.
干杯...!!!
Wajira Weerasinghe.
我有一个每天每件商品的表 - 存储日期,商品ID,销售数量和销售价格(即使它也在产品表中存储 - 如果更改,您需要你在保存时实际售出的价值).您可以在查询中计算每个项目日的总计和每天的总计.
表:
create table product (
id integer primary key,
name varchar(100) not null,
price decimal(6,2) not null,
inventory integer not null
);
create table sale (
saledate date not null,
product_id integer not null references product,
quantity integer not null,
price decimal(6,2) not null,
primary key (saledate, product_id)
);
Run Code Online (Sandbox Code Playgroud)
报告一天:
select s.product_id, p.name, s.quantity, s.price, (s.quantity * s.price) as total
from product p, sale s
where p.id = s.product_id
and s.saledate = date '2010-12-5';
Run Code Online (Sandbox Code Playgroud)
报告所有日子:
select saledate, sum(quantity * price) as total
from sale
group by saledate
order by saledate;
Run Code Online (Sandbox Code Playgroud)
一个很好的主日历报告,带有摘要行:
select *
from (
(select s.saledate, s.product_id, p.name, s.quantity, s.price, (s.quantity * s.price) as total
from product p, sale s
where p.id = s.product_id)
union
(select saledate, NULL, 'TOTAL', sum(quantity), NULL, sum(quantity * price) as total
from sale group by saledate)
) as summedsales
order by saledate, product_id;
Run Code Online (Sandbox Code Playgroud)