ADO SQL 使用 group by 查询的结果更新表

发布于 2024-09-01 04:08:52 字数 275 浏览 10 评论 0原文

我正在尝试使用包含相同值的记录数来更新 .mdb 表中的记录。

下面的 SQL 不起作用,但我认为它表明了我想要实现的目标。

UPDATE table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT  FROM table1 GROUP BY PH_BSP)  AS B
ON A.PH_BSP=B.PH_BSP
SET A.PH_SORT = B.PHCOUNT;

有什么想法吗?

I am trying to update records in an .mdb table with the number of records containing the same value.

The SQL below does not work but I think gives an indication of what I am trying to achieve.

UPDATE table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT  FROM table1 GROUP BY PH_BSP)  AS B
ON A.PH_BSP=B.PH_BSP
SET A.PH_SORT = B.PHCOUNT;

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

银河中√捞星星 2024-09-08 04:08:52

如果在 Access 中执行此操作,则需要使用域聚合函数:

UPDATE table1 
SET PH_SORT = DCount("PH_BSP","Table1","PH_BSP='" & PH_BSP & "'")

上面假设 PH_BSP 是文本字段,如果是数字,则删除单引号。

If you are doing this in Access, you need to use a domain aggregate function:

UPDATE table1 
SET PH_SORT = DCount("PH_BSP","Table1","PH_BSP='" & PH_BSP & "'")

The above assumes that PH_BSP is a text field, drop the single quotes if it is numeric.

世界如花海般美丽 2024-09-08 04:08:52

未经测试,但因此列出声明这应该可以解决您的问题

UPDATE A
SET A.PH_SORT = B.PHCOUNT
From table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT  FROM table1 GROUP BY PH_BSP)  AS B
ON A.PH_BSP=B.PH_BSP

编辑:
您的问题可能来自您的子查询,我会尝试将该部分放入单独的访问查询中,看看它是如何进行的。根据记忆,我曾经在 Access 和子查询方面遇到很多麻烦,方括号有时也会有所帮助,但从记忆来看不太可靠。

Untested, but setting out the statement thusly this should solve your issue

UPDATE A
SET A.PH_SORT = B.PHCOUNT
From table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT  FROM table1 GROUP BY PH_BSP)  AS B
ON A.PH_BSP=B.PH_BSP

Edit:
Your problem might be from your sub query, I would try putting that part into a separate Access Query and see how it goes. From memory I used to have a lot of trouble with Access and subqueries, square brackets would also sometimes help, but unreliable from memory.

晚风撩人 2024-09-08 04:08:52

你尝试过类似的事情吗?

update table1 as a
    set a.ph_sort = (
        select COUNT(b.ph_bsp) as phcount
            from table1 b
            where b.ph_bsp = a.ph_bsp)

我假设这里是 SQL Server。

但我想这个或类似的东西应该可以做到。

Have you tried something alike?

update table1 as a
    set a.ph_sort = (
        select COUNT(b.ph_bsp) as phcount
            from table1 b
            where b.ph_bsp = a.ph_bsp)

I'm assuming SQL Server here.

But this or something alike should do it, I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文