sql - 数据库设计:具有多级包装的库存
问题描述
我正在尝试为我的库存开发一个数据库。但我不知道如何跟踪多级包装。
例如:
我目前有一个产品和职位表
产品
Id | Name
================
1013 | Metal
1014 | Wood
职位
id | Name
================
1 | 1-1-1-1
2 | 1-1-1-2
我的库存表我正在考虑做这样的事情:假设我在位置 1-1-1-1 存储了 1 个 1000 金属的盒子和 1 个 500 木材的盒子
ItemId | ProductId | Quantity | PositionId
==========================================
1 | 1013 | 1000 | 1
2 | 1014 | 500 | 1
所以我将分别用条形码 1 和 2 标记这两个盒子,所以如果我扫描它们,我可以查看这张表以查看其中的产品和数量。
但我也可以将这 2 个盒子(1 和 2)放在另一个盒子(我们称之为盒子 3)中,这将为它生成一个新的条形码,如果扫描,它将显示以前的盒子及其项目。并将这个盒子 3 存放在另一个位置
我也可以把这个盒子 3 放在一个托盘里,生成一个新的代码等等。所以基本上我可以多级打包N次。
跟踪所有这些的最佳表结构是什么?提前感谢您的帮助!
解决方案
我能够建立一个良好的结构:我的产品和职位是相同的,但我创建了一个库存表,如:
id | product_id | amount | parent_id | position_id
=====================================================
1 | 1013 | 1000 | 4 | 1
2 | 1013 | 1000 | 4 | 1
3 | 1014 | 500 | 4 | 1
4 | 1234 | NULL | NULL | 1
1234(随机 id)是一个包含 2000 个金属和 500 个木材的盒子。我不将此框保存在产品表中。
当我扫描 id 为 3 的框时,我执行递归 cte 查询:
with recursive bom as (
select *, 1 as level
from testing.stock
where id = '4' #scanned id
union all
select c.*, p.level + 1
from testing.stock c
join bom p on c.parent_id = p.id
)
select product_id as product, sum(amount), position_id
from bom b
left join testing.product pd on b.product_id = pd.id
where pd.id is not null
group by product_id, position_id
返回:
sum | product | position
2000 | 1013 | 3
500 | 1014 | 3
为了获得位置,我只是运行上述查询的变体。要执行更新,我在该框中获取 Id 并运行
update testing.stock set position = '2' where id in (#variation of above query)
我希望这可以帮助别人。这适用于 N 包装级别
推荐阅读
- java - RecyclerView 未加载
- plugins - Gimp:如何批量调整多个尺寸的一张图像并导出它们?
- node.js - 带有 child_process NodeJS 的 JavaScript 事件源
- c# - 使用我的 WPF GUI 冻结的 Powershell 脚本无法找出原因
- python - AWS Canary Selenium 用户代理字符串
- python - 如何打印一个数组中相对于另一个数组中的值的值
- android - 无法安装 SDK 工具,完成按钮被禁用
- git - 如何修复 git 错误:RPC 失败;HTTP 520 卷曲 22?
- powershell - 导出 CSV 文件的格式与实际命令不同
- discord.py - 当我们不是服务器的所有者时,我们如何添加机器人?