【问题标题】:Access db loop - for each record in one table create array of records in another table访问数据库循环 - 为一个表中的每条记录在另一个表中创建记录数组
【发布时间】:2010-10-22 22:34:56
【问题描述】:

是否可以在 Access DB 中创建嵌套循环查询来更新第三个表?

我有一个主(标题)表:

------------------------
masters
------------------------
num | modality | cost  |
------------------------
01  | thing    | 23.00 |
02  | thing    | 42.00 |
03  | thing    | 56.00 |
04  | apple    | 11.00 |
05  | apple    | 17.00 |

和一个包含详细信息的临时表,我需要创建第三个(实际)details 表,该表将关闭 ma​​sters

这是临时详细信息表的示例。

----------------------------------
temps
----------------------------------
modelnumber | modality | priceEa |
----------------------------------
| 123       | thing    | 1.00    |
| 234       | apple    | 2.00    |
| 345       | apple    | 3.00    |
| 456       | apple    | 4.00    |
| 567       | thing    | 5.00    |

基本上,我需要遍历 ma​​sters 表中的每条记录。

外循环:

对于 masters 表中的每条记录,获取模态。

内循环:

然后对于 temps 表中的每条记录,其中方式匹配,在 details 表中创建一条记录(并在此过程中,根据 temps.priceEa 和 masters.cost 进行一些计算)。

这应该为 masters 表中的每条记录在 details 表中创建 (masters * temps) 数量的新记录。

详细信息表,最终应该看起来像

----------------------------------------------------------
details
----------------------------------------------------------
num  | modelnumber | modality | priceEa  |  adjustedCost |
----------------------------------------------------------
| 01 | 123         | thing     | 1.00    | (do calc here)
| 01 | 567         | thing     | 5.00    | (do calc here)
| 02 | 123         | thing     | 1.00    | (do calc here)
| 02 | 567         | thing     | 5.00    | (do calc here)
| 03 | 123         | thing     | 1.00    | (do calc here)
| 03 | 567         | thing     | 5.00    | (do calc here)
| 04 | 234         | apple     | 2.00    | (do calc here)
| 04 | 345         | apple     | 3.00    | (do calc here)
| 04 | 456         | apple     | 4.00    | (do calc here)
| 05 | 234         | apple     | 2.00    | (do calc here)
| 05 | 345         | apple     | 3.00    | (do calc here)
| 05 | 456         | apple     | 4.00    | (do calc here)
...etc

【问题讨论】:

    标签: database ms-access nested-loops


    【解决方案1】:
    SELECT m.num, t.modelnumber, m.modality, t.priceea into myNewTempTable from masters m inner join temp t on m.modality = t.modality order by m.num, t.modelnumber

    【讨论】:

    • 我想你搞砸了!有时 sql 如何执行循环更新/插入而不真正看起来会这样做,这并不好笑......它让我的大脑通过错误的方式思考问题。谢谢。
    • 呃,SQL 在概念上不执行循环。如果您考虑一下 SQL 一次更新/插入所有受影响的行,这可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2023-01-17
    • 1970-01-01
    • 2020-12-28
    • 2023-02-04
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多