修复孤立记录的 UPDATE 查询
我有一个 Access 数据库,其中有两个通过 PK/FK 关联的表。不幸的是,数据库表允许重复/冗余记录,并使数据库有点混乱。我正在尝试找出一条可以解决该问题的 SQL 语句。
为了更好地解释问题和目标,我创建了示例表作为参考: 替代文本 http://img38.imageshack.us/img38/9243/514201074110am.png< /a> 您会注意到有两个表,一个 Student 表和一个 TestScore 表,其中 StudentID 是 PK/FK。
Student 表包含学生 John、Sally、Tommy 和 Suzy 的重复记录。换句话说,StudentID 为 1 和 5 的 John 是同一个人,Sally 2 和 6 是同一个人,依此类推。
TestScore 表将测试分数与学生相关联。
忽略 Student 表如何/为什么允许重复等 - 我试图实现的目标是更新 TestScore 表,以便它用相应的启用的 StudentID 替换已禁用的 StudentID。因此,所有 StudentID = 1 (John) 将更新为 5;所有 StudentID = 2 (Sally) 将更新为 6,依此类推。这是我正在拍摄的结果 TestScore 表(请注意,不再有任何对禁用的 StudentID 1-4 的引用): 替代文本 http://img163.imageshack.us/img163/1954/514201091121am.png< /a> 您能想到一个可以实现此目标的查询(与 MS Access 的 JET 引擎兼容)吗?或者,也许您可以提供一些提示/观点,为我指明正确的方向。
谢谢。
I have an Access database that has two tables that are related by PK/FK. Unfortunately, the database tables have allowed for duplicate/redundant records and has made the database a bit screwy. I am trying to figure out a SQL statement that will fix the problem.
To better explain the problem and goal, I have created example tables to use as reference:
alt text http://img38.imageshack.us/img38/9243/514201074110am.png
You'll notice there are two tables, a Student table and a TestScore table where StudentID is the PK/FK.
The Student table contains duplicate records for students John, Sally, Tommy, and Suzy. In other words the John's with StudentID's 1 and 5 are the same person, Sally 2 and 6 are the same person, and so on.
The TestScore table relates test scores with a student.
Ignoring how/why the Student table allowed duplicates, etc - The goal I'm trying to accomplish is to update the TestScore table so that it replaces the StudentID's that have been disabled with the corresponding enabled StudentID. So, all StudentID's = 1 (John) will be updated to 5; all StudentID's = 2 (Sally) will be updated to 6, and so on. Here's the resultant TestScore table that I'm shooting for (Notice there is no longer any reference to the disabled StudentID's 1-4):
alt text http://img163.imageshack.us/img163/1954/514201091121am.png
Can you think of a query (compatible with MS Access's JET Engine) that can accomplish this goal? Or, maybe, you can offer some tips/perspectives that will point me in the right direction.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
做到这一点的唯一方法是通过一系列查询和临时表。
首先,我将创建以下 Make Table 查询,您将使用它来创建错误 StudentID 到正确 StudentID 的映射。
接下来,您将使用该临时表用正确的 StudentID 更新 TestScore 表。
The only way to do this is through a series of queries and temporary tables.
First, I would create the following Make Table query that you would use to create a mapping of the bad StudentID to correct StudentID.
Next, you would use that temporary table to update the TestScore table with the correct StudentID.
识别表中重复项的最常见技术是按表示重复记录的字段进行分组:
在本例中,我们要删除其中一条 Brian Smith 记录,或者在您的情况下,更新 ID 字段,以便它们都具有值25 或 1(完全任意使用哪一个)。
在 ID 上使用 min 将返回:
如果你使用 max 你会得到
我通常使用这种技术来删除重复项,而不是更新它们:
The most common technique to identify duplicates in a table is to group by the fields that represent duplicate records:
In this case we want to remove one of the Brian Smith Records, or in your case, update the ID field so they both have the value of 25 or 1 (completely arbitrary which one to use).
Using min on ID will return:
If you use max you would get
I usually use this technique to delete the duplicates, not update them: