【发布时间】:2016-01-27 16:47:07
【问题描述】:
我有一个带有 id 的 table1。另一个 table2 有两个字段; 主和子。
我需要从table1中选择first Id作为table2中的master,而 table2 中的其他 id 为 child。
我试过这个:
Dim rs as recordset
Dim strSql as String
strSql = "Select top 1 Id FROM table1"
Set rs = CurrentDb.OpenRecordset(strSql)
rs.MoveFirst
strSql = "INSERT INTO table2 (child) SELECT table1.Id FROM table1 WHERE Id <> " & rs.Fields(0).Value
DoCmd.RunSQL strSql
strSql = "UPDATE table2 SET master = " & rs.Fields(0).Value & " WHERE master is null"
DoCmd.RunSQL strSql
rs.Close
Set rs = Nothing
但是由于我必须在代码中多次对不同的表执行相同的操作,因此如果可能的话,我宁愿使用一个查询来执行此操作。如果可能,也可以不使用子查询。
【问题讨论】:
-
你将如何在
table1中定义first Id -
@VR46,最低 id。或第一个。
-
这个问题没有显示任何研究工作。
-
使用 VBA。 DMin() 查找最小值。 id,然后是带有 id + 所有其他行的 INSERT。