MS-ACCESS:删除除前 1 行之外的所有行并通过查询更新表

发布于 2024-07-20 06:41:46 字数 1142 浏览 3 评论 0原文

我已经快完成这件事了,只剩下最后几个小问题了。 我现在需要从表中删除除前 1 条记录之外的所有记录,其中 Readings_miu_id 是“DISTINCT”列。 换句话说,我需要删除表中除第一个 DISTINCT Readings_miu_id 之外的所有记录。 我假设我需要做的就是修改基本删除语句:

DELETE FROM analyzedCopy2
WHERE readings_miu_id = some_value

但我不知道如何将 some_column=some_value 部分更改为类似:

where some_column notequal to (select top 1 from analyzedCopy2 as A 
where analyzedCopy2.readings_miu_id = A.readings_miu_id)

然后我需要弄清楚如何使用 UPDATE 语句来更新来自查询的表(analyzedCopy2)(这是我想要存储到表analyzedCopy2中的RSSI列中的所有值当前所在的位置)。 我已经尝试过:

UPDATE analyzedCopy2 from testQuery3 SET analyzedCopy2.RSSI = 
(select AvgOfRSSI from testQuery3 INNER JOIN  analyzedCopy2 on analyzedCopy2.readings_miu_id =  testQuery3.readings_miu_id where analyzedCopy2.readings_miu_id = testQuery3.readings_miu_id) 
where analyzedCopy2.readings_miu_id = testQuery3.readings_miu_id

但显然我不能在更新语句中使用 FROM 。 有什么想法吗?

我确信我正在以一种非常不标准的方式(如果不是的话,可能是完全错误的)方式解决这个问题,但我不被允许使用 vb.net2008 来提取和操作然后存储数据,就像我想要的那样所以我现在陷入了在 ms-access 中使用 sql 语句的困境,这是一次很好的学习经历(即使尝试做一些像我在 sql 语句中所做的奇怪的事情让我用头撞我的甲板< em>当然是象征性的)

I'm almost done with this, just a few last hiccups. I now need to delete all records from a table except for the top 1 where readings_miu_id is the "DISTINCT" column. In other words words i need to delete all records from a table other than the first DISTINCT readings_miu_id. I am assuming all I need to do is modify the basic delete statement:

DELETE FROM analyzedCopy2
WHERE readings_miu_id = some_value

But I can't figure out how to change the some_column=some_value part to something like:

where some_column notequal to (select top 1 from analyzedCopy2 as A 
where analyzedCopy2.readings_miu_id = A.readings_miu_id)

and then I need to figure out how to use an UPDATE statement to update a table (analyzedCopy2) from a query (which is where all of the values I want stored into column RSSI in table analyzedCopy2 are currently located). I've tried this:

UPDATE analyzedCopy2 from testQuery3 SET analyzedCopy2.RSSI = 
(select AvgOfRSSI from testQuery3 INNER JOIN  analyzedCopy2 on analyzedCopy2.readings_miu_id =  testQuery3.readings_miu_id where analyzedCopy2.readings_miu_id = testQuery3.readings_miu_id) 
where analyzedCopy2.readings_miu_id = testQuery3.readings_miu_id

but apparently I can't use FROM inside of an update statement. Any thoughts?

I'm sure I'm going about this a very nonstandard (and possibly if not probably the flat out wrong) way but I'm not being allowed to use vb.net2008 to pull and manipulate then store the data like I would like to so I'm stuck right now using sql statements in ms-access which is a good learning experience (Even if trying to do such odd things as I've been having to do in sql statements is making me beat my head against my deck figuratively of course)

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

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

发布评论

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

评论(3

淡淡的优雅 2024-07-27 06:41:46

MS Access UPDATE sql 语句无法引用查询,但可以引用表。 所以要做的就是将查询结果存储到表中。

SELECT YourQuery.* 
INTO TempTable1
FROM YourQuery

现在您可以在 UPDATE 查询中使用 TempTable1:

UPDATE TargetTable
INNER JOIN TempTable1 ON TempTable1.TargetTableId = TargetTable.Id
SET TargetTable.TargetField = TempTable1.SourceField

请参阅我对此问题的回答

MS Access UPDATE sql statements cannot reference queries, but they can reference tables. So the thing to do is store the query results into a table.

SELECT YourQuery.* 
INTO TempTable1
FROM YourQuery

Now you can use TempTable1 in an UPDATE query:

UPDATE TargetTable
INNER JOIN TempTable1 ON TempTable1.TargetTableId = TargetTable.Id
SET TargetTable.TargetField = TempTable1.SourceField

See my answer to this question.

撩心不撩汉 2024-07-27 06:41:46

我在这台机器上没有访问权限的副本,自从我涉足访问权限以来已经有几年了,所以我在这里进行了一次疯狂的尝试,但是你可以做一个

delete from analyzedCopy2
where readings_miu_id not in (select top 1 readings_miu_id from analyzedCopy2 order by...)

(你需要 order by 才能获得正确的前 1 条记录,也许可以按 id 排序?)

如果没有访问副本,我没有希望帮助您处理第二条记录。 我知道如何在 TSQL 中做到这一点,但访问是一个完全不同的 wtf 的水壶:-)

I don't have a copy of access on this machine, and it's been a few years since I dabbled in access, so I'm taking a wild stab here, but can you do a

delete from analyzedCopy2
where readings_miu_id not in (select top 1 readings_miu_id from analyzedCopy2 order by...)

(you'll need the order by to get the proper top 1 record, order by the id maybe?)

I've got no hope of helping you with the second one without a copy of access. I know how I'd do it in TSQL, but access is a whole different kettle of wtf's :-)

鱼忆七猫命九 2024-07-27 06:41:46

我试图让事情变得太复杂,因为我需要提取的所有记录在每个字段中都有相同的信息,我需要做的就是使用:

   SELECT DISTINCT readings_miu_id, DateRange, RSSI, ColRSSI, Firmware, CFGDate, FreqCorr, Active, OriginCol, ColID, Ownage, SiteID, PremID, prem_group1, prem_group2
FROM analyzedCopy2   
ORDER BY readings_miu_id;

为了提取每个 Readings_miu_id 的前 1 条记录。

I was trying to make too complicated, since all of the records that i needed to pull had the same information in each field that i needed all i had to do was use:

   SELECT DISTINCT readings_miu_id, DateRange, RSSI, ColRSSI, Firmware, CFGDate, FreqCorr, Active, OriginCol, ColID, Ownage, SiteID, PremID, prem_group1, prem_group2
FROM analyzedCopy2   
ORDER BY readings_miu_id;

in order to pull the top 1 record per readings_miu_id.

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