在 UPDATE 查询中使用 SELECT
如何使用 Microsoft Access 2007 中 SELECT
查询的结果UPDATE
表的字段。
这是选择查询:
SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS
WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))
GROUP BY FUNCTIONS.Func_ID;
这是更新查询:
UPDATE FUNCTIONS
SET FUNCTIONS.Func_TaxRef = [Result of Select query]
How can I UPDATE
a field of a table with the result of a SELECT
query in Microsoft Access 2007.
Here's the Select Query:
SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS
WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))
GROUP BY FUNCTIONS.Func_ID;
And here's the Update Query:
UPDATE FUNCTIONS
SET FUNCTIONS.Func_TaxRef = [Result of Select query]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
嗯,看来 Access 无法在 UPDATE 查询中进行聚合。 但它可以在 SELECT 查询中进行聚合。 因此,创建一个具有如下定义的查询:
并将其保存为 YourQuery。 现在我们必须解决另一个访问限制。 UPDATE 查询不能对查询进行操作,但可以对多个表进行操作。 因此,让我们使用 Make Table 查询将查询转换为表:
这会将视图的内容存储在名为 MinOfTax_Code 的表中。 现在您可以执行更新查询:
在 Access 中执行 SQL 有点困难,我会为您的项目研究 Sql Server Express Edition!
Well, it looks like Access can't do aggregates in UPDATE queries. But it can do aggregates in SELECT queries. So create a query with a definition like:
And save it as YourQuery. Now we have to work around another Access restriction. UPDATE queries can't operate on queries, but they can operate on multiple tables. So let's turn the query into a table with a Make Table query:
This stores the content of the view in a table called MinOfTax_Code. Now you can do an UPDATE query:
Doing SQL in Access is a bit of a stretch, I'd look into Sql Server Express Edition for your project!
我写了一些有关 Access/JET SQL 中相关子查询的限制的文章不久前,注意到连接多个表进行 SQL UPDATE 的语法。 根据该信息和一些快速测试,我认为没有任何方法可以在单个 SQL UPDATE 语句中使用 Access/JET 执行您想要的操作。 如果可以的话,该语句将如下所示:
另外,Access/JET 有时会让您将子查询保存为单独的查询,然后以更传统的方式将其连接到 UPDATE 语句中。 因此,例如,如果我们将上面的 SELECT 子查询保存为名为 FUNCTIONS_TAX 的单独查询,则 UPDATE 语句将是:
但是,这仍然不起作用。
我相信完成这项工作的唯一方法是将最小 Tax_Code 值的选择和聚合移至带外。 您可以使用 VBA 函数来完成此操作,或者使用 Access DLookup 函数更轻松地完成此操作。 将上面的 GROUP BY 子查询保存到名为 FUNCTIONS_TAX 的单独查询中,并将 UPDATE 语句重写为:
请注意,DLookup 函数可防止在 Access 外部使用此查询,例如通过 JET OLEDB。 此外,这种方法的性能可能非常糟糕,具体取决于您的目标行数,因为正在为每个 FUNCTIONS 行执行子查询(因为,当然,它不再相关,这就是顺序的全部要点使其发挥作用)。
祝你好运!
I wrote about some of the limitations of correlated subqueries in Access/JET SQL a while back, and noted the syntax for joining multiple tables for SQL UPDATEs. Based on that info and some quick testing, I don't believe there's any way to do what you want with Access/JET in a single SQL UPDATE statement. If you could, the statement would read something like this:
Alternatively, Access/JET will sometimes let you get away with saving a subquery as a separate query and then joining it in the UPDATE statement in a more traditional way. So, for instance, if we saved the SELECT subquery above as a separate query named FUNCTIONS_TAX, then the UPDATE statement would be:
However, this still doesn't work.
I believe the only way you will make this work is to move the selection and aggregation of the minimum Tax_Code value out-of-band. You could do this with a VBA function, or more easily using the Access DLookup function. Save the GROUP BY subquery above to a separate query named FUNCTIONS_TAX and rewrite the UPDATE statement as:
Note that the DLookup function prevents this query from being used outside of Access, for instance via JET OLEDB. Also, the performance of this approach can be pretty terrible depending on how many rows you're targeting, as the subquery is being executed for each FUNCTIONS row (because, of course, it is no longer correlated, which is the whole point in order for it to work).
Good luck!
我有类似的问题。 我想在一列中找到一个字符串,并将该值放入同一个表的另一列中。 下面的 select 语句查找括号内的文本。
当我在 Access 中创建查询时,我选择了所有字段。 在该查询的 SQL 视图中,我将 mytable.myfield 替换为我想要从括号内获取值的字段,并
运行了 make table 查询。 make table 查询具有上述替换的所有字段,并以
INTO NameofNewTable FROM mytable
结尾I had a similar problem. I wanted to find a string in one column and put that value in another column in the same table. The select statement below finds the text inside the parens.
When I created the query in Access I selected all fields. On the SQL view for that query, I replaced the mytable.myfield for the field I wanted to have the value from inside the parens with
I ran a make table query. The make table query has all the fields with the above substitution and ends with
INTO NameofNewTable FROM mytable
这有效吗? 未经测试,但应该能表达要点。
基本上,对于 FUNCTIONS 中的每一行,子查询确定当前最低税码并将 FUNCTIONS.Func_TaxRef 设置为该值。 这是假设 FUNCTIONS.Func_ID 是主键或唯一键。
Does this work? Untested but should get the point across.
Basically for each row in FUNCTIONS, the subquery determines the minimum current tax code and sets FUNCTIONS.Func_TaxRef to that value. This is assuming that FUNCTIONS.Func_ID is a Primary or Unique key.
我确实想再添加一个利用 VBA 函数的答案,但它确实可以通过一条 SQL 语句完成这项工作。 不过,它可能会很慢。
I did want to add one more answer that utilizes a VBA function, but it does get the job done in one SQL statement. Though, it can be slow.
我知道这个主题很旧,但我想我可以添加一些东西。
我无法在 MS Access 2010 中使用 SQL 来使用 Select 查询进行更新。我使用 Tomalak 的建议来完成这项工作。 我有一个屏幕截图,但显然我是这个网站上的新手,无法发布它。
我可以使用查询设计工具来完成此操作,但即使当我查看已确认成功的更新查询时,Access 也无法向我显示实现此操作的 SQL。 所以我无法单独使用 SQL 代码来完成这项工作。
我创建了选择查询并将其保存为单独的查询。 在查询设计工具中,我添加了我试图更新已保存的选择查询的表(我将唯一键放入选择查询中,因此它们之间有链接)。 正如托马拉克所建议的,我将查询类型更改为更新。 然后我只需选择我要更新的字段(并指定表)。 在“更新到”字段中,我输入了引入的选择查询中的字段名称。
这种格式成功并更新了原始表。
I know this topic is old, but I thought I could add something to it.
I could not make an Update with Select query work using SQL in MS Access 2010. I used Tomalak's suggestion to make this work. I had a screenshot, but am apparently too much of a newb on this site to be able to post it.
I was able to do this using the Query Design tool, but even as I was looking at a confirmed successful update query, Access was not able to show me the SQL that made it happen. So I could not make this work with SQL code alone.
I created and saved my select query as a separate query. In the Query Design tool, I added the table I'm trying to update the the select query I had saved (I put the unique key in the select query so it had a link between them). Just as Tomalak had suggested, I changed the Query Type to Update. I then just had to choose the fields (and designate the table) I was trying to update. In the "Update To" fields, I typed in the name of the fields from the select query I had brought in.
This format was successful and updated the original table.