如何使用 SQL 管理数据库中的 FIFO 队列?
我的数据库中有两张表,一张用于In
,一张用于Out
。它们有两列:数量
和价格
。如何编写选择正确价格的 SQL 查询?
例如:如果我有 3 个项目 in 为 75,然后有 3 个项目 in 为 80。那么我有两个 out 为 75,并且第三个出局应为 75 (X),第四个出局应为 80 (Y)。
如何编写 X 和 Y 的价格查询? 他们应该使用第三行和第四行的价格。例如,有什么方法可以选择 In
表中的第三行吗?我不能使用 auto_increment 作为“第三”行的标识符,因为这些表也将包含其他项目的帖子。这些行不会被删除,它们将出于责任原因而被保存。
SELECT Price FROM In WHERE ...?
NEW 数据库设计:
+----+
| In |
+----+------+-------+
| Supply_ID | Price |
+-----------+-------+
| 1 | 75 |
| 1 | 75 |
| 1 | 75 |
| 2 | 80 |
| 2 | 80 |
+-----------+-------+
+-----+
| Out |
+-----+-------+-------+
| Delivery_ID | Price |
+-------------+-------+
| 1 | 75 |
| 1 | 75 |
| 2 | X | <- ?
| 3 | Y | <- ?
+-------------+-------+
OLD 数据库设计:
+----+
| In |
+----+------+----------+-------+
| Supply_ID | Quantity | Price |
+-----------+----------+-------+
| 1 | 3 | 75 |
| 2 | 3 | 80 |
+-----------+----------+-------+
+-----+
| Out |
+-----+-------+----------+-------+
| Delivery_ID | Quantity | Price |
+-------------+----------+-------+
| 1 | 2 | 75 |
| 2 | 1 | X | <- ?
| 3 | 1 | Y | <- ?
+-------------+----------+-------+
I have two tables in my database, one for In
and one for Out
. They have two columns, Quantity
and Price
. How can I write a SQL-query that selects the correct price?
In example: If I have 3 items in for 75 and then 3 items in for 80. Then I have two out for 75, and the third out should be for 75 (X) and the fourth out should be for 80 (Y).
How can I write the price query for X and Y? They should use the price from the third and forth row. In example, is there any way to SELECT the third row in the In
-table? I can not use auto_increment as identifier for i.e. "third" row, because the tables will contain post for other items too. The rows will not be deleted, they will be saved for accountability reasons.
SELECT Price FROM In WHERE ...?
NEW database design:
+----+
| In |
+----+------+-------+
| Supply_ID | Price |
+-----------+-------+
| 1 | 75 |
| 1 | 75 |
| 1 | 75 |
| 2 | 80 |
| 2 | 80 |
+-----------+-------+
+-----+
| Out |
+-----+-------+-------+
| Delivery_ID | Price |
+-------------+-------+
| 1 | 75 |
| 1 | 75 |
| 2 | X | <- ?
| 3 | Y | <- ?
+-------------+-------+
OLD database design:
+----+
| In |
+----+------+----------+-------+
| Supply_ID | Quantity | Price |
+-----------+----------+-------+
| 1 | 3 | 75 |
| 2 | 3 | 80 |
+-----------+----------+-------+
+-----+
| Out |
+-----+-------+----------+-------+
| Delivery_ID | Quantity | Price |
+-------------+----------+-------+
| 1 | 2 | 75 |
| 2 | 1 | X | <- ?
| 3 | 1 | Y | <- ?
+-------------+----------+-------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读评论,您说您愿意添加自动增量或日期字段来了解每行的正确位置。添加此后,我建议在 In 表中再添加一行名为“processed”的行,当该行添加到表中时,该行会自动设置为 false。任何已复制到 OUT 的行都已将其已处理字段设置为 true。
然后,要找到下一个要移至 OUT 的项目,您可以执行以下操作:
一旦该行移至 OUT,您只需将该行的已处理字段更新为 true。
Reading the comments you say that you are willing to add a auto increment or date field to know the proper position of each row. Once you add this I would recommend adding one more row to the In table called processed which is automatically set to false when the row is added to the table. Any rows that have been copied to OUT already have their processed filed set to true.
Then to find the next item to move to OUT you can do
Once the row is moved to OUT then you just UPDATE the processed field of that row to true.
我看不到在这里有帮助的简单查询。为了在 SQL 中模拟 FIFO,我将查看三个表:OPERATION、OUT 和 FIFO。 OPERATION 实际上是事务的日志,而 FIFO 表是 FIFO 状态,而 OUT 是来自 FIFO 的响应。
您将在操作(添加和删除项目)进入 OPERATION 表时更新 CURRENT,并处理对 OUT 表中的项目“输出”的请求,减少 FIFO 中的值,并在必要时从 FIFO 表中删除记录。
即使如此,我也没有看到一个简单的查询来处理整个事情,因为需要查询第一条记录以查看每个操作是否有足够的数量,适当地更新该记录并查询无法执行该操作的其他记录实现了。我的 SQL 能力水平并不能引导我找到一个简单的解决方案,它对我来说构成了业务逻辑并被提升到该层。
I can't see a simple query that would help here. To simulate a FIFO in SQL I would look to three tables, OPERATION, OUT and FIFO. OPERATION is effectively a log of the transactions and the FIFO table is the FIFO state and OUT is the responses from the FIFO.
You would update CURRENT with the operations (adding and removing items) as they come into the OPERATION table and process requests for item 'out's into the OUT table decrementing the values in the FIFO and where necessary removing the records from the FIFO table.
Even then I don't see a simply query to process the whole thing as there's a need to query the first record to see if there is sufficient quantity for each operation, updating of that record appropriately and querying of additional records where the operation cannot be fulfilled. My level of SQL ability doesn't lead me to a simple solution to that, it constitutes business logic to me and gets lifted into that tier.