另一个涉及组合的复杂 MySQL 查询
假设我们有一个如下所示的表。
connection_requirements
+-----------------------------------+
| item_id | connector_id | quantity |
+---------+--------------+----------+
| 1 | 4 | 1 |
| 1 | 5 | 1 |
| 1 | 2 | 2 |
+---------+--------------+----------+
该表列出了电子设备运行所需的连接器,以及每种类型的连接器需要多少个。 (想想主板上的连接需要来自电源的某些类型的连接器)
现在我们还有这个表...
connections_compatability
+-------------------------+
| connector_id | works_as |
+--------------+----------+
| 6 | 4 |
| 6 | 5 |
+--------------+----------+
其中第一列是连接器,也可以充当第二列的连接器 ID。 (例如,电源具有“6+2 针”等连接器,可以用作“8 针”或“6 针”)
现在,我们终于知道此表中有多少个可用的连接器
connector_quantities
+-------------------------+
| connector_id | quantity |
+--------------+----------+
| 1 | 1 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
| 6 | 4 |
| 7 | 0 |
| 8 | 5 |
+--------------+----------+
根据这些表,如下所示您可以推断,我们确实有足够的连接器供第 1 项正常运行。尽管我们没有足够的连接器 #5,但我们有 4 个连接器 #6,它们可以用作连接器 #4 和 #5。
connection_requirements 表连接到 items 表,我们如何过滤需要比我们可用连接更多的连接的项目?我们已经准备好代码来过滤需要不可用连接器的项目。
这个问题有更多的复杂性,所以我们试图简化问题。
非常感谢所有的帮助!
Lets say we have a table that looks like this
connection_requirements
+-----------------------------------+
| item_id | connector_id | quantity |
+---------+--------------+----------+
| 1 | 4 | 1 |
| 1 | 5 | 1 |
| 1 | 2 | 2 |
+---------+--------------+----------+
This table is a list of connectors that a electronic device requires to operate, and how many of each type of connector it requires. (Think connections on a motherboard requiring certain types of connectors from a power supply)
Now we also have this table...
connections_compatability
+-------------------------+
| connector_id | works_as |
+--------------+----------+
| 6 | 4 |
| 6 | 5 |
+--------------+----------+
Where the first column is the connector that can also act as the connector id of the second column. (For instance a power supply has connectors such as "6+2 Pin" which can work as "8 Pin" or "6 Pin")
Now finally we have how many of each connectors are available in this table
connector_quantities
+-------------------------+
| connector_id | quantity |
+--------------+----------+
| 1 | 1 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
| 6 | 4 |
| 7 | 0 |
| 8 | 5 |
+--------------+----------+
Based off these tables, as you can infer, we do have enough connectors for item number 1 to properly operate. Even though we do not have enough of connector #5, we have 4 connector #6s, which can work as connector #4 and #5.
The connection_requirements table is joined onto the items table, how can we filter items that require more connections than we have available? We already have the code in place to filter items that require connectors that are unavailable.
The problem has many more layers of complexity to it, so we tried to simplify the problem.
Much appreciation for all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是确定物品的“真实”库存,包括其替代品。例如,第 4 部分的实际库存实际上是 5:1-第 4 部分 + 4-第 6 部分。因此,使用它:
这种方法当然有一个问题。假设您有一个产品的组成为:
4 #4 连接器和 2 #6 连接器
。从技术上讲,您确实有 4 个 #4 连接器(1 个 #4 和 3 个 #6 替代品),但结合 2 个 #6 连接器的要求,您没有足够的零件。要解决此问题,您可能必须使用循环或多个查询来确定用完所有主要零件后的现有库存。One approach is to determine the "real" inventory of items including their substitutions. E.g., the real inventory of part 4 is actually 5: 1-part #4 + 4-part #6. So using that:
There is of course a catch with this approach. Suppose you have an item with a makeup of:
4 #4 connectors and 2 #6 connectors
. Technically, you do have 4 #4 connectors (1 #4, and 3 #6 substitutions) but in combination with the requirement of 2 #6 connectors, you do not have enough parts. To solve this problem you would likely have to use a loop or multiple queries which would determine on-hand inventory after you use up all your primary parts.